与Vue.js的第八天

今天学习了Vue组件中的非父子之间的传值生命周期
Vue组件之间的传值分三种
1.父传子之间传值用属性:props
2.子传父之间传值用方法:自定义方法
3.非父子之间传值用第三方。

非父子之间传值实例

<body>
    <div id="demo">
        <first></first>
        <second></second>
        
    </div>
    <script src="js/vue.js"></script>
    <script>
        var bus=new Vue();
        Vue.component('first',{
            template:`
                <div>
                    <h2>4987489789<h2>
                    <button @click='btn'>点击</button>
                </div>
            `,
            data:function(){
                return{
                    msg:'hellow word'
                }
            },
            methods:{
                btn:function(){
                    bus.$emit('send',this.msg)
                }
            }
        })
        Vue.component('second',{
            template:`
                <div>
                    <h2>sdafsdfs</h2>
                    <a href="">{{mess}}</a>
                </div>
            `,
            data:function(){
                return{
                    mess:''
                }
            },
            mounted:function(){
                bus.$on('send',msg=>{
                    this.mess=msg
                })
            }
        })
        new Vue({
            el:'#demo'
        })
    </script>
</body>

生命周期
20170303180741807.png

生命周期分8个阶段
1.beforeCreate(创建前)
2.created(创建后)
3.beforeMount(载入前)
4.mounted(载入后)
5.beforeUpdate(更新前)
6.updated(更新后)
7.beforeDestroy(销毁前)
8.destroyed(销毁后)

实例

<body>
   <div id='app'>{{msg}}</div>
    <script src='js/vue.js'></script>
    <script>
       new Vue({
           el:'#app',
           data:{
               msg:'hello vue'
           },
           beforeCreate:function(){
               alert('beforeCreated');
           },
           created:function(){
               alert('created')
           },
           beforeMount:function(){
                 alert('beforMount')
           },
           mounted:function(){
               alert('mounted')
           }
       })
    </script>
</body>

作业(昨天)

父子组件通信

<body>
    <div id="demo">
        <lzy></lzy>
    </div>
    <script src="js/vue.js"></script>
    <script>
        Vue.component('lzy',{
            template:`
                <div>
                    <ul>
                        <li v-for='(value,index) in arr'>{{value}}</li>
                    </ul>
                    <cont @send='newBtn' addName='jake'></cont>
                    <cont @send='newBtn' addName='jan'></cont>
                </div>
            `,
            data:function(){
                return{
                    arr:[]
                }
            },
            methods:{
                newBtn:function(txt){
                    this.arr.push(txt)
                }
            }
        })
        Vue.component('cont',{
            props:['addName'],
            template:`
                <div>
                    <label>{{addName}}</label>
                    <input type="text" v-model='add'>
                    <button @click='btn'>点击</button>
                </div>
            `,
            data:function(){
                return{
                    add:''
                }
            },
            methods:{
                btn:function(){
                    this.$emit('send',this.addName+':'+this.add)
                }
            }
            
        })
        new Vue({
            el:'#demo'
        })
    </script>
</body>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容