您好,登錄后才能下訂單哦!
小編給大家分享一下Vue實(shí)現(xiàn)多層組件嵌套的方法有哪些,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
實(shí)例如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 測(cè)試實(shí)例-組件嵌套二種方式</title> <script src="//cdn.bootcss.com/vue/2.1.10/vue.js"></script> </head> <body> <div id="app"> <Itemlist1> <Item v-for="item in items1" :data="item"/> </Itemlist1> <Itemlist2 :itemlist="items2"></Itemlist2> </div> <script> Vue.component('Item',{ template: '<div>{{data.name}}</div>', props: { data:Object } }); // 方式一:嵌套組件時(shí)用<slot></slot>, Vue.component("Itemlist1", { template: '<div @click="ok"><slot></slot></div>', props: { itemList: Array }, methods: { ok: function() { alert(this.abc); } } }); // 方式二: Vue.component("Itemlist2", { template: '<div @click="ok"><Item v-for="item in itemlist" :data="item"/></div>', props: { itemlist: Array }, methods: { ok: function() { alert(this.abc); } } }); // 創(chuàng)建根實(shí)例 var vueApp = new Vue({ el: '#app', data: { items1: [{ 'name': "item1" }, { 'name': "item2" }, { 'name': "item3" }], items2: [{ 'name': "item1-1" }, { 'name': "item2-1" }, { 'name': "item3-1" }] } }) </script> </body> </html>
看完了這篇文章,相信你對(duì)“Vue實(shí)現(xiàn)多層組件嵌套的方法有哪些”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。