Vue 组件 / 创建

全局组件
Vue.component("tem",{
    template:`
        <div>
            <h3>全局组件</h3>
        </div>
        `
})
局部组件
<body>
<div id="app">
    <child></child>
</div>

<template id="child">
    <div>
        <p>this's chlidTem</p>
    </div>
</template>
<script>
    new Vue({
        components:{
            "child":{
                template:"#child"
            }
        }
    }).$mount("#app")
</script>
</body>
动态组件

通过所使用保留的<component>元素,动态的绑定到它的is特性,我们可以让多个组件可以使用同一个挂载点,并且动态切换

<body>
<div id="app">
    <div>
        <input type="button" 
               v-for="item in inputs"  
               :value="item.index" :index="item.index" 
               @click="showTem($event)">
    </div>
    <component :is="show"></component>
</div>
<script>
    new Vue({
        data:{
            inputs:{
                first:{
                    index:"first"
                },
                second:{
                    index:"second"
                },
                third:{
                    index:"third"
                }
            },
            show:"first"
        },
        methods:{
            showTem(e){
                this.show=e.target.getAttribute("index");
            }
        },
        components:{
            "first":{
                template: `
                    <div>this's first</div>
                `
            },
            "second":{
                template:`
                    <div>this's second</div>
                `
            },
            "third":{
                template:`
                   <div>this's third</div>
                `
            }
        }
    }).$mount("#app")
//由上我们可以看出,实际上动态组件的本质就是通过:is来控制组件的显示/隐藏,当然除了通过不同的按钮来控制,也可以通过单个按钮的click方法为show赋不同的值来改变模板的显示/隐藏
</script>
</body>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Vue 实例 属性和方法 每个 Vue 实例都会代理其 data 对象里所有的属性:var data = { a:...
    云之外阅读 2,258评论 0 6
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,083评论 0 29
  • 此文基于官方文档,里面部分例子有改动,加上了一些自己的理解 什么是组件? 组件(Component)是 Vue.j...
    陆志均阅读 3,870评论 5 14
  • 1.安装 可以简单地在页面引入Vue.js作为独立版本,Vue即被注册为全局变量,可以在页面使用了。 如果希望搭建...
    Awey阅读 11,109评论 4 129
  • 我长得那么丑,没人看上的,男朋友听到问了:那我呢?我答:你长得太帅了,所以喜欢我!男朋友无语了
    易多咪阅读 207评论 0 0