vue父子组件通信

1.非父子之间传值

非父子之间传值用第三方

   <div id='app'>
       <chat></chat>
   </div>
    <script src="vue/dist/vue.js"></script>
    <script>
    Vue.component('chat',{
        template:`
           <div>
              <ul>
                 <li v-for="value in arr">{{value}}</li>
              </ul> 
              <user @send='rcvMsg' userName='jack'></user>
              <user @send='rcvMsg' userName='rose'></user>
           </div>
       `,
        data:function(){
            return{
                arr:[]
            }
        },
        methods:{
            rcvMsg:function(txt){
                this.arr.push(txt)
            }
        }
    })  
    
    Vue.component('user',{
        props:['userName'],
        template:`
          <div>
             <label>{{userName}}</label>
             <input type='text' v-model='inputVal'>
             <button @click='sendMsg'>发送</button>
          </div>
        `,
        data:function(){
            return{
                inputVal:''
            }
        },
        methods:{
          sendMsg:function(){
              this.$emit('send',this.userName+':'+this.inputVal)
          }
        }
    }) 
    new Vue({
        el:'#app'
    })
    </script>
2.生命周期

每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期。

生命周期的八个过程:

1.beforeCreate(创建前)
2.created(创建后)
3.beforeMount(载入前)
4.mounted(载入后)
5.beforeUpdate(更新前)
6.updated(更新后)
7.beforeDestroy(销毁前)
8.destroyed(销毁后)

<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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 首先认识一下vue的目录结构。vue是一个组件化开发的语言,组件之间是有父子关系的,还有一种关系叫全局组件和局部...
    峰峰峰峰峰_689e阅读 727评论 0 1
  • 感觉Vue中父子传参的方式,实在是太多了,于是做一个小总结,只是总结我所知道的。 1.父传子 基本就用一个方式,p...
    _Storm阅读 2,238评论 0 1
  • 在使用vue的过程中,如果需要进行父子组件间的通信,通过查阅官方文档我们可以了解到只需要在子组件要显式地用 pro...
    fisher_zh阅读 472评论 0 1
  • Vue父子组件通信 Web中的组件其实就是页面组成的一部分。 那组件之间的通信该怎么办?这是个重点(必须掌握),同...
    程序员之路阅读 663评论 0 2
  • 一个员工在基础岗位上干了3年,因为能力突出,得到上司的青睐,被提升为主管。他欣喜若狂,终于当上了leader,从个...
    乐活的聪头阅读 217评论 0 0