以下是一個被封裝的box組件,在父組件中點擊該組件可以實現打開一個新頁面:
Box.vue
1 <template> 2 <div class="box"> 3 <div class="grid-content bg-purple"> 4 <div 5 class="title-style" 6 @click="openUrl(url)" 7 >{{title}}</div> 8 </div> 9 </div> 10 </template> 11 12 <script> 13 export default { 14 props: { 15 title: { 16 type: String, 17 default: 'hello world' 18 }, 19 url:{ 20 type:String, 21 default:'https://www.baidu.com' 22 } 23 }, 24 methods:{ 25 openUrl(url){ 26 window.open(url,"_blank"); 27 //_blank : 在新窗口打開 28 //_self : 在當前窗口打開 29 30 //window.location.href = url : 當前頁面重定向 31 } 32 } 33 } 34 </script> 35 36 <style> 37 </style>