同级传值

同级传值——非父子关系,借助第三方量

例子:

<!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>

父子组件通信例子:

<!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>

生命周期:
Vvue-js-的生命周期_03.gif

它可以总共分为8个阶段:

beforeCreate(创建前),

created(创建后),

beforeMount(载入前),

mounted(载入后),

beforeUpdate(更新前),

updated(更新后),

beforeDestroy(销毁前),

destroyed(销毁后)

定义:

Vue实例有一个完整的生命周期,也就是从开始创建、初始化数据、编译模板、挂载Dom、渲染→更新→渲染、卸载等一系列过程,我们称这是Vue的生命周期。通俗说就是Vue实例从创建到销毁的过程,就是生命周期。

图解生命周期

特别值得注意的是created钩子函数和mounted钩子函数的区别
31738308-58e8726fbe4d0_articlex.jpg

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

例子:

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

推荐阅读更多精彩内容

  • 一:什么是闭包?闭包的用处? (1)闭包就是能够读取其他函数内部变量的函数。在本质上,闭包就 是将函数内部和函数外...
    xuguibin阅读 9,763评论 1 52
  • # 传智播客vue 学习## 1. 什么是 Vue.js* Vue 开发手机 APP 需要借助于 Weex* Vu...
    再见天才阅读 3,639评论 0 6
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 别有春情惆怅意 奈何秋风不解语 眼前榕江滚滚流 寄予丹心不知去
    躺在沙发上的Lee阅读 584评论 0 2
  • 所以写到这里。终于明白我心里的最爱还是唱歌。 刚刚踏入社会,参加工作的前两年,是糊里糊涂年轻着的,走到哪里就把歌带...
    我是秦简阅读 303评论 0 1