非父子组件的通信

非父子组件通信-ref

案列:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <Father/>
  </div>

  <template id="father">
    <div>
      <h3>这里是father组件</h3>
      <button @click = "printThis"> 输出this </button>
      <hr>
      <Girl :fn = "emitCry"></Girl>
      <hr>
      <Son ref = "son"></Son>
    </div>
  </template>

  <template id = "son">
    <div>
      <h3> 这里是son组件 </h3>
      <img  v-if = "flag" style = "width: 100px;height: 100px;" src="https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1564471527&di=2c4707955cefb24c325842ea288b9647&src=http://b-ssl.duitang.com/uploads/item/201702/16/20170216145320_wyNiK.jpeg" alt="">
    </div>
  </template>


  <template id="girl">
    <div>
      <h3> 这里是girl 组件 </h3>
      <button @click = "fn"> 揍弟弟 </button>
    </div>
  </template>

</body>
<script src="../../person/vue.js"></script>
<script>
  Vue.component('Father',{
    template: '#father',
    methods: {
      printThis ( ) {
        console.log( this )
      },
      emitCry () {
        this.$refs.son.cry()
      }
    }
  })
  Vue.component('Son',{
    template: '#son',
    data () {
      return {
        flag: false
      }
    },
    methods: {
      cry () {
        this.flag = true
      }
    }
  })
  Vue.component('Girl',{
    template: '#girl',
    props: ['fn']
  })
  new Vue({
    el: '#app'
  })
</script>
</html>

非父子通信-bus

案列:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <Girl></Girl>
    <Son></Son>
  </div>

  <template id="girl">
    <div>
      <h3> 这里是girl </h3>
      <button @click = "kick"> 揍弟弟 </button>
    </div>
  </template>

  <template id="son">
    <div>
      <h3> 这里是son </h3>
      <img style = "width: 100px;height: 100px;" v-if = "flag" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564481613744&di=22ebae5b3c4655691a1244e46682d7c9&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201702%2F16%2F20170216145320_wyNiK.jpeg" alt="">
    </div>
  </template>




</body>
<script src="../../lib/vue.js"></script>


<script>
  var bus = new Vue()

  console.log( bus )

  Vue.component('Son',{
    template: '#son',
    data () {
      return {
        flag: false
      }
    },
    methods: {
      cry () {
        this.flag = true
      }
    },
    mounted () { //这个选项表示组件挂载结束   这个方法时自定执行,只要组件创建,它就自动执行
      // bus.$on(自定义事件的名称,自定义事件的处理程序)

      var  _this = this 
      bus.$on('aa',function () { // 通过$on自定义一个叫做aa的事件 
        _this.cry()
      })
    }
  })

  Vue.component('Girl',{
    template: '#girl',
    methods: {
      kick () {
        bus.$emit('aa')
      }
    }
  })

  new Vue({
    el: '#app'
  })
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值