天天看點

vue2.0中組建裡面套用元件_vue2.0如何嵌套元件

顯示子元件資料

{{ msg }}

{{ msg }}

在2.0中這樣使用浏覽器會報錯:[Vue warn]: Component template should contain exactly one root element:

缺少根元素?我試了下好像隻有2.0才會報錯,這是什麼原因?

補上js代碼:

Vue.component('parent-component',{

template: '#parent-component',

components: {

'child1': {

template: '#child-component1',

data: function(){

return {

msg: 'child1'

};

}

},

'child2': {

template: '#child-component2',

data: function(){

return {

msg: 'child2'

};

}

}

},

methods: {

showChildComponentData: function(){

for( var i = 0;i < $children.lenth;i++) {

alert($children[i].msg);

}

}

}

});

new Vue({

el: '#app'

});