美文网首页
父子组件通信 非父子组件传值 生命周期

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

作者: LVLIN_1598 | 来源:发表于2018-09-23 19:47 被阅读0次

案例:

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

相关文章

  • VUE组件(传值,生命周期)

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

  • vue中的组件通信

    一、组件通信(组件传值) 1.1父子组件通信 1.2子父组件通信 1.3非父子组件通信(兄弟组件通信)

  • 生命周期,组件通信、插槽

    一、生命周期 二、组件通信 Vue只有两种关系, 父子组件 非父子组件 1.父子通信:当我们需要把父组件中的数据传...

  • 同级传值

    同级传值——非父子关系,借助第三方量 例子: 父子组件通信例子: 生命周期:Vvue-js-的生命周期_03.gi...

  • VUE03

    Vue组件 组件的创建 组件的指令以及事件绑定 父子组件创建 父子组件通信 兄弟组件的传值 动态组件

  • Vue组件通信

    父组件传值子组件 子组件传值父组件 非父子组件通信 发布订阅模式 / 总线模式 我们使用一个空的Vue实例作为总线...

  • 09-生命周期及非父子组件间的通信

    一. Vue生命周期 二、生命周期代码 三、非父子组件通信 vue中非父子组件通信需要借助一个空的vue实例,案...

  • 2018-09-25组件

    组件分为:子传父父传子非父子之间的通信 1.子传父 2.父传子 3.非父子之间传值

  • Vue父子组件通信和双向绑定

    本篇文章主要介绍父子组件传值,组件的数据双向绑定。 1. 基础父子组件传值 父子组件传值,这是Vue组件传值最常见...

  • Composition API的使用

    目标 父子组件传值props 和 context 祖孙组件传值provice和inject 生命周期 on**...

网友评论

      本文标题:父子组件通信 非父子组件传值 生命周期

      本文链接:https://www.haomeiwen.com/subject/wlexoftx.html