美文网首页Vue
Vue 组件 / 创建

Vue 组件 / 创建

作者: 羊烊羴 | 来源:发表于2018-03-19 13:31 被阅读0次
全局组件
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组件Vue组件的创建vue.extend结合vue.component创建vue.component...

  • Vue组件和父子组件

    【一】Vue组件化 创建组件构造器对象Vue.extend() 创建组件构造器const cpnc = Vue.e...

  • vue.component、vue.extend、vue

    vue.component 注册全局组件 vue.extend 创建组件构造器 vue.component(组件名...

  • VUE03--{组件、生命周期}

    1.创建组件 Vue.extend、Vue.component结合创建 Vue.component创建 templ...

  • router - 2018-06-25

    2018-06-25 创建 vue异步组件技术 vue-router配置路由,使用vue的[异步组件](组件 — ...

  • vue-cli兄弟组件传值文章

    1.创建两个兄弟组件分别是header.vue组件和middle.vue组件 1.创建一个父组件chuanzhi....

  • VUE03

    Vue组件 组件的创建 组件的指令以及事件绑定 父子组件创建 父子组件通信 兄弟组件的传值 动态组件

  • Vue 笔记(二)- 从 component 到 slot

    组件 定义组件:使用Vue.extend(options)创建,其中options和new Vue(options...

  • Vue组件化开发

    一.如何创建Vue全局组件 特点:在任何一个Vue控制的区域都能使用1.创建组件构造器注意点:创建组件模板的时候只...

  • (15)打鸡儿教你Vue.js

    组件化vue.js 组件单向绑定组件双向绑定组件单次绑定 创建组件构造器注册组件使用组件 Vue.extend()...

网友评论

    本文标题:Vue 组件 / 创建

    本文链接:https://www.haomeiwen.com/subject/rdtpqftx.html