最近在研究vue組件的學(xué)習(xí),給自己留個(gè)筆記,也分享給大家,希望能對大家有所幫助
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>Vue 測試實(shí)例-組件嵌套二種方式</title> <script src="https://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>以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯(cuò)新站長站。
新聞熱點(diǎn)
疑難解答
圖片精選