2019-02-18 vue组件基础篇6

  • 组件高级用法
    递归组件

组件在它的模板内可以递归地调用自己,只要给组件设置name的选项就可以了。设置name后,在组件模板内就可以递归使用了,不过需要注意的是,必须给一个条件来限制递归数量,否则会抛出错误: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>

内联模板

在自定义组件中添加inline-template,组件就会把其内容当做template模板。
在父组件中声明的数据message和子组件中声明的数据msg,两个都可以渲染(如果同名,优先使用子组件的数据)。这反而是内联模板的缺点,就是作用域比较难理解,如果不是非常特殊的场景,建议不要轻易使用内联模板。

<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: '在子组件声明的数据'
                }
            }
        });
        
        var app = new Vue({
            el: '#app',
            data: {
                messages: '在父组件声明的数据'
            }
        })
</script>

动态组件

Vue.js提供了一个特殊的元素<component>用来动态地挂载不同的组件,使用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>

动态地改变currentView的值就可以动态挂载组件了。也可以直接绑定在组件对象上。

<div id="app">
        <component :is="addComp"></component>
</div>
<script>
        var app = new Vue({
            el: '#app',
            data: {
                addComp: 'coma'
            },
            components: {
                coma: {
                    template: '<div>动态挂载组件</div>'
                }
            }
        })
</script>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 组件(Component)是Vue.js最核心的功能,也是整个架构设计最精彩的地方,当然也是最难掌握的。...
    六个周阅读 10,936评论 0 32
  • 什么是组件? 组件 (Component) 是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素,封装...
    youins阅读 13,167评论 0 13
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 10,463评论 0 29
  • 此文基于官方文档,里面部分例子有改动,加上了一些自己的理解 什么是组件? 组件(Component)是 Vue.j...
    陆志均阅读 9,240评论 5 14
  • Vue 实例 属性和方法 每个 Vue 实例都会代理其 data 对象里所有的属性:var data = { a:...
    云之外阅读 6,660评论 0 6