父子组件通信 非父子组件传值 生命周期

2018-09-23  本文已影响0人  LVLIN_1598

案例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
   <div id='app'>
     <chat></chat>
   </div>
<script src='js/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>
</body>
</html>

非父子组件传值:
案例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
   <div id='app'>
   <child></child>
   <son></son>
   </div>
<script src='js/vue.js'></script>
<script>
   var bus=new Vue();
   Vue.component('child',{//A
       template:`
        <div>
           <h1>我是child组件</h1>
           <button @click='sendMsg'>发送数据给son</button>
        </div>
       `,
       data:function(){
           return{
               msg:'hello vue'
           }
       },
       methods:{
           sendMsg:function(){
              bus.$emit('send',this.msg) 
           }
       }
   }) 
    
   Vue.component('son',{//B
       template:`
        <div>
           <h1>我是son组件</h1>
           <a href=''>{{mess}}</a>
        </div>
       `,
       data:function(){
           return{
               mess:''
           }
       },
       mounted:function(){
           bus.$on('send',msg=>{//箭头函数
               console.log(this);
               this.mess=msg
           })
       }
       
   }) 
    
   new Vue({
       el:'#app'
   })
</script>
</body>
</html>

生命周期:在Vue的整个生命周期中,它提供了一系列的事件,可以让我们在事件触发时注册js方法,可以让我们用自己注册的js方法控制整个大局,在这些事件响应方法中的this直接指向的是vue的实例。
beforeCreate(创建前),

created(创建后),

beforeMount(载入前),

mounted(载入后),

beforeUpdate(更新前),

updated(更新后),

beforeDestroy(销毁前),

destroyed(销毁后)

案例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<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>
</html>
上一篇 下一篇

猜你喜欢

热点阅读