张蕾的技术博客

vue学习大纲4-vue组件详细解读

2017-06-16  本文已影响277人  cd72c1240b33

全局组件

//注册全局组件
Vue.component('leilei',{
    template:'<h1>你好,{{msg}}</h1>',
    //在组件中,data是个函数,返回的值才是我们的数据
    //每个组件都有自己的方法,返回自己的对象;
    data(){
        return{
            msg:'蕾蕾'
        }
    }
})
var app1=new Vue({
    el:'#app1'
});
var app2=new Vue({
    el:'#app2'
})

局部组件

var app=new Vue({
    el:'#app',
    components:{// 一个实例中,可以定义多个组件
        hello:{
            template:'<h2>{{msg}}!ymy!!</h2>',
            data(){
                return{
                    msg:'hello'
                }
            }
        }
    }
})

父子组件

//这种是错误的,因为到时候都会被父组件的模版替换掉,所以,我们应该在父组件的模版中使用子组件;
<parent>
    <children></chilren>
</parent>
<template id="templ">
    <div>
        <h2>parent</h2>
        <children></children>
    </div>
</template>
var app=new Vue({
    el:'#app',
    components:{
        parent:{//父组件
            template:'#templ',
            components:{
                children:{//子组件
                    template:'<h3>children</h3>'
                }
            }
        },

    }
})

父组件给子组件传递数据-通过props


<template id="templ">
    <div>
        <h2>parent</h2>
        <children :aa="name"></children>
    </div>
</template>
components:{
    parent:{
        template:'#templ',
        data(){
            return {
                name:'hello,leilei'
            }
        },
        components:{
            children:{
                //props:{aa:String} 子组件校验父组件传过来的数据,必须为字符串类型
                props:{aa:String},//props:['aa'] 子组件只接收父组件传来的数据,不校验
                template:'<h3>children {{aa}}</h3>'
            }
        }
    }
}

子组件给组件传递数据- 通过events

传递步骤如下
1、 先通过子组件内部的data函数,写好子组件的数据;当子组件点击事件发生的时候,通过this.$emit给父组件发射自己的数据

this.$emit('s',this.name);

2、 在子组件名字上,添加这个s事件

<children @s="getName"></children>

3、父组件在自己的methods方法中,通过getName接收子组件传递过来的数据,通过改变自己的name值;

getName(data){
    this.name=data;
}

所有代码如下

<template id="templ">
    <div>
        <h2>parent {{name}}</h2>
        <children @s="getName"></children>
    </div>
</template>
<script src="js/vue2.js"></script>
<script>
    var app=new Vue({
        el:'#app',
        components:{
            parent:{
                template:'#templ',
                data(){
                    return{
                        name:'tangtang'
                    }
                },
                methods:{
                    getName(data){
                        this.name=data;
                    }
                },
                components:{
                    children:{
                        template:'<h3 @click="changename">children</h3>',
                        data(){
                            return {
                                name:'Vivian'
                            }
                        },
                        methods:{
                            changename(){
                                //子组件发射一个事件给父组件,告诉自己的名字
                                this.$emit('s',this.name)
                            }
                        }
                    }
                }
            },

        }
    })
</script>

子组件更改父组件数据,数据同步

data(){
    return {
        name:{name:'hello,leilei'}
    }
}

子组件更改父组件数据,但是不同步

components:{
        children:{
            //props:{aa:String} 子组件校验父组件传过来的数据,必须为字符串类型
            props:{aa:String},//props:['aa'] 子组件只接收父组件传来的数据,不校验
            template:'<h3 @click="change">children {{bb}}</h3>',
            data(){
               return{
                   bb:this.aa
               }
            },
            mounted(){
                this.bb=this.aa
            },
            methods:{
                change(){
                    this.bb='vivian';
                }
            }
        }
    }
}

组件的is特性:即动态组件

<component v-bind:is="currentView">
  <!-- 组件在 vm.currentview 变化时改变! -->
</component>
<div id="app">
    <button @click="comp='t1'">显示组件1</button>
    <button @click="comp='t2'">显示组件2</button>
    <component :is="comp"></component>
</div>
var app=new Vue({
    el:'#app',
    data:{
        comp:'t1' //:is="comp" 他决定显示哪个组件
    },
    components:{//这是定义了两个组件
        t1:{
            template:'#temp1'
        },
        t2:{
            template:'#temp2'
        }
    }
})

原始内容的插槽 slot

<div id="app">
    <hello>圆梦源你好!</hello>
</div>
<template id="t1">
    <div>hello! <slot></slot></div>
</template>
<div id="app">
    <hello>
        <div slot="d1">圆梦源你好!</div>
        <div slot="d2">张蕾</div>
    </hello>
</div>
<template id="t1">
    <div>hello! <slot name="d2"></slot><slot name="d1"></slot></div>
</template>

非父子组件数据传递:类似于原生中的"订阅发布"

var Event=new Vue;//事件; Event.$on Event.$emit
var app=new Vue({
    el:'#app',
    components:{
        hello1:{
            template:'#t1',
            methods:{
                send(){//发射事件
                    Event.$emit('myEvent','中午吃点好的')
                }
            }
        },
        hello2:{
            template:'#t2',
            data(){
                return {msg:""}
            },
            mounted(){//提前绑定好的事件
                Event.$on('myEvent',(a)=>{
                    this.msg=a;
                })
            }
        }
    }
})
上一篇 下一篇

猜你喜欢

热点阅读