2019-02-18 vue組件基礎(chǔ)篇6

  • 組件高級用法
    遞歸組件

組件在它的模板內(nèi)可以遞歸地調(diào)用自己,只要給組件設(shè)置name的選項就可以了。設(shè)置name后,在組件模板內(nèi)就可以遞歸使用了,不過需要注意的是,必須給一個條件來限制遞歸數(shù)量,否則會拋出錯誤:max stack size exceeded。

<div id="app">
        <child-component :count="1"></child-component>
</div>
<script>
        Vue.component('child-component', {
            name: 'child-component',
            props: {
                count: {
                    type: Number,
                    default: 1
                }
            },
            template: '<div class="child"> \
                                <child-component :count="count+1" v-if="count < 3"> \
                                </child-component> \
                            </div>'
        })

        var app = new Vue({
            el: '#app'
        })
</script>

內(nèi)聯(lián)模板

在自定義組件中添加inline-template,組件就會把其內(nèi)容當做template模板。
在父組件中聲明的數(shù)據(jù)message和子組件中聲明的數(shù)據(jù)msg,兩個都可以渲染(如果同名,優(yōu)先使用子組件的數(shù)據(jù))。這反而是內(nèi)聯(lián)模板的缺點,就是作用域比較難理解,如果不是非常特殊的場景,建議不要輕易使用內(nèi)聯(lián)模板。

<div id="app">
        <child-component inline-template :message="messages">
            <div>
                <h2>在父組件中定義子組件的模板</h2>
                <p>{{ message }}</p>
                <p>{{ msg }}</p>
            </div>
        </child-component>
</div>
<script>
        Vue.component('child-component', {
            props: ['message'],
            data: function() {
                return {
                    msg: '在子組件聲明的數(shù)據(jù)'
                }
            }
        });
        
        var app = new Vue({
            el: '#app',
            data: {
                messages: '在父組件聲明的數(shù)據(jù)'
            }
        })
</script>

動態(tài)組件

Vue.js提供了一個特殊的元素<component>用來動態(tài)地掛載不同的組件,使用is特性來選擇要掛載的組件。

<div id="app">
        <component :is="currentView"></component>
        <button @click="handleChangeView('A')">切換到A</button>
        <button @click="handleChangeView('B')">切換到B</button>
        <button @click="handleChangeView('C')">切換到C</button>
</div>
<script>
        var app = new Vue({
            el: '#app',
            components: {
                comA: {
                    template: '<div>組件A</div>'
                },
                comB: {
                    template: '<div>組件B</div>'
                },
                comC: {
                    template: '<div>組件C</div>'
                },
            },
            data: {
                currentView: 'comA'
            },
            methods: {
                handleChangeView: function (component) {
                    this.currentView = 'com' + component;
                }
            }
        })
</script>

動態(tài)地改變currentView的值就可以動態(tài)掛載組件了。也可以直接綁定在組件對象上。

<div id="app">
        <component :is="addComp"></component>
</div>
<script>
        var app = new Vue({
            el: '#app',
            data: {
                addComp: 'coma'
            },
            components: {
                coma: {
                    template: '<div>動態(tài)掛載組件</div>'
                }
            }
        })
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 組件(Component)是Vue.js最核心的功能,也是整個架構(gòu)設(shè)計最精彩的地方,當然也是最難掌握的。...
    六個周閱讀 5,770評論 0 32
  • 什么是組件? 組件 (Component) 是 Vue.js 最強大的功能之一。組件可以擴展 HTML 元素,封裝...
    youins閱讀 9,708評論 0 13
  • 這篇筆記主要包含 Vue 2 不同于 Vue 1 或者特有的內(nèi)容,還有我對于 Vue 1.0 印象不深的內(nèi)容。關(guān)于...
    云之外閱讀 5,177評論 0 29
  • 此文基于官方文檔,里面部分例子有改動,加上了一些自己的理解 什么是組件? 組件(Component)是 Vue.j...
    陸志均閱讀 3,949評論 5 14
  • Vue 實例 屬性和方法 每個 Vue 實例都會代理其 data 對象里所有的屬性:var data = { a:...
    云之外閱讀 2,373評論 0 6

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