1-12 vue父子組件

vue父子組件

  • 父子組件
    • 組件意味著協(xié)同工作,通常父子組件會(huì)是這樣的關(guān)系:組件 A 在它的模版中使用了組件 B 。
    • 最簡(jiǎn)單的父子組件
<body>
    <div id="app">
        <parent-template></parent-template>
        <parent-template2></parent-template2>

        <my-child></my-child>
    </div>
</body>

<script src="js/vue.js"></script>
<script>

    //1.子組件構(gòu)造器
    var Child = Vue.extend({
        template:'![](images/pic1.jpeg)'
    });

    //2.父組件構(gòu)造器
    var Parent = Vue.extend({
        //注冊(cè)子組件
       components:{
           'child-template':Child
       },
        template:'<div><child-template></child-template><p>照片很模糊</p></div>'
    });

    var Parent2 = Vue.extend({
        components:{
            'child-template':Child
        },
        template:'<div><p>下面的照片很清晰!</p><child-template></child-template></div>'
    });

    //3.創(chuàng)建組件
    Vue.component('parent-template', Parent);
    Vue.component('parent-template2', Parent2);

    //注冊(cè)組件
    Vue.component('my-child', Child);
    Vue.component('parent-component', Child);

    //4.vue實(shí)例
    new Vue({
        el:'#app'
    });

</script>

  • 在父子組件組合使用中要注意以下一些問(wèn)題:
> 沒(méi)有實(shí)例化的子組件不能拿出來(lái)單獨(dú)使用!

```
<div id="app">
   <parent-component></parent-component>
   <child-component></child-component>
</div>
```

> 在父標(biāo)簽內(nèi)部嵌套子標(biāo)簽
<div id="app">
    <parent-component>
        <child-component></child-component>
    </parent-component>
 </div>

因?yàn)樵诟笜?biāo)簽一旦生成真實(shí)的DOM,其內(nèi)部的子標(biāo)簽就會(huì)被解析成為普通的HTML標(biāo)簽來(lái)執(zhí)行,而且<child-component>不是標(biāo)準(zhǔn)的HTML標(biāo)簽,會(huì)被瀏覽器過(guò)濾掉。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Vue 實(shí)例 屬性和方法 每個(gè) Vue 實(shí)例都會(huì)代理其 data 對(duì)象里所有的屬性:var data = { a:...
    云之外閱讀 2,394評(píng)論 0 6
  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對(duì)于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,187評(píng)論 0 29
  • 此文基于官方文檔,里面部分例子有改動(dòng),加上了一些自己的理解 什么是組件? 組件(Component)是 Vue.j...
    陸志均閱讀 3,955評(píng)論 5 14
  • 下載安裝搭建環(huán)境 可以選npm安裝,或者簡(jiǎn)單下載一個(gè)開(kāi)發(fā)版的vue.js文件 瀏覽器打開(kāi)加載有vue的文檔時(shí),控制...
    冥冥2017閱讀 6,210評(píng)論 0 42
  • 1.安裝 可以簡(jiǎn)單地在頁(yè)面引入Vue.js作為獨(dú)立版本,Vue即被注冊(cè)為全局變量,可以在頁(yè)面使用了。 如果希望搭建...
    Awey閱讀 11,312評(píng)論 4 129

友情鏈接更多精彩內(nèi)容