美文网首页
Vue如何实现组件通信?

Vue如何实现组件通信?

作者: HeroMeikong | 来源:发表于2018-10-21 10:18 被阅读0次

Vue组件通信的三种情况:

  • 父子通信
  • 爷孙通信
  • 兄弟通信
  1. 父子通信:父组件使用Prop向子组件传递数据,子组件通过事件向父组件发送消息,使用 v-on 绑定自定义事件)
    <div id="app">
      <p>{{message}}</p>
      <button @click="visible=true">打开</button>
      <child v-show=visible @close="visible=false"></child>
    </div>
    
    Vue.component('child',{
      template: `
        <div>我是儿子
          <button @click="$emit('close')">关闭</button>
        </div>`
    })
    new Vue({
      el: '#app',
      data: {
        message: 'hello',
        visible: false
      }
    })
    
  2. 爷孙通信(通过两对父子通信,爷爸之间父子通信,爸儿之间父子通信)
    <div id="app">
      <p>{{message}}</p>
      <button @click="seen=true">打开</button>
      <child v-show=seen  @close="log(seen)"></child>
    </div>
    
    Vue.component('child',{
      props: ['seen','seensunzi'],
      template: `
        <div>
          我是爸爸<button  @click="seensunzi=true">打开</button>
          <sunzi v-show=seensunzi @close="seensunzi=false;$emit('close')"></sunzi>
        </div>`
    })
    Vue.component('sunzi',{
      props: ['seensunzi'],
      template: `
        <div>
          我是儿子<button @click="$emit('close')">关闭</button>
        </div>`
    })
    var vue = new Vue({
      el: '#app',
      data: {
        message: '我是爷爷',
        seen: false,
        seensunzi: false
      },
      methods:{
        log(seen){
          console.log('爷爷收到消息:爷爷先关掉儿子,再关掉我爸爸')
          setTimeout(()=>{
            this.seen = false
          },2000)
        }
      }
    })
    
  3. 兄弟通信(new Vue() 作为 eventBus)
    <div id="app">
      <component-a></component-a>
      <component-b></component-b>
    </div>
    
    let eventHub = new Vue()
    Vue.prototype.$eventHub = eventHub
    
    Vue.component('component-a',{
      template:`
        <div>a<button @click=notify>click</button></div>
      `,
      methods:{
        notify(){
          this.__proto__ === Vue.prototype
          this.$eventHub.$emit('xxx','hi')
        }
      }
    })
    Vue.component('component-b',{
      template:`
        <div>b<div ref=output></div></div>
      `,
      created(){
        this.$eventHub.$on('xxx',(data)=>{
          this.$refs.output.textContent = data
        })
      }
    })
    let app = new Vue({
      el: "#app"
    })
    

相关文章

  • Vue之数据通信

    Vue 如何实现组件通信?①父组件向子组件通信(props:['属性名']) 给父组件中的子组件标签绑定属性,然后...

  • Vue如何实现组件通信?

    Vue组件通信的三种情况: 父子通信 爷孙通信 兄弟通信 父子通信:父组件使用Prop向子组件传递数据,子组件通过...

  • Vue入门-实现一个简陋的todolist

    Vue中通过props和$emit实现父子组件的通信。

  • 父子组件通信

    vue之父子组件间通信实例讲解(props、emit) Vue.js 递归组件实现树形菜单(实例分享)

  • Vue组件通信大全(终结篇)

    背景   Vue是单页面应用,单页面应用又是由组件构成,各个组件之间又互相关联,那么如何实现组件之间通信就显得尤为...

  • Vue 组件间通信

    背景 在使用 Vue 时,经常会有父子组件之间的通信需求,有很多种方法可以实现。 实现 父组件向子组件通信 方法一...

  • vue组件间通信

    vue组件间通信 1.父组件向子组件传递数据--props 在vue2.0中使用props实现向子组件传递数据: ...

  • vue

    1、什么是组件化、有什么好处、vue如何创建组件、vue组件之间如何通信 什么是组件化。任何一个页面我们都可以抽象...

  • 手写事件总线eventBus

    在vue中,我们有时会用eventBus进行简易组件通信,那么这个eventBus究竟是如何实现的呢?eventB...

  • Vue组件通信

    最近在学习Vue,组件化后不同组件之间如何通信,记录一下。 Vue中组件通信时,数据流只能由父级传递给子级,通过p...

网友评论

      本文标题:Vue如何实现组件通信?

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