v-bind="$attrs" v-on="$listeners" 多层组件监听

vue组件间的通信

多级组件通信,用vuex太重,props太麻烦。

vue 2.4 版本提供了另一种方法,使用 v-bind=”$attrs”, 将父组件中不被认为 props特性绑定的属性传入子组件中,通常配合 interitAttrs 选项一起使用。

<top>
    <center>
        <bottom>
        </bottom>
    </center>
</top>

需求:top和bottom间进行通信

  1. props和$emit,需要center作为中转

  2. vuex,确定是全局状态?

上点片段,只讲用法

<top @getDataList ="getDataList" :yes="123"><top>

//    .native绑原生事件是没用的
<center v-on="$listeners" v-bind="$attrs"></center>

//在bottom组件中,可以直接调用getDataList这个方法
export default {
  name: 'index',
  props: ['yes'],
  inheritAttrs: false,
  methods: {
    doClick(data) {
      console.log(this.yes) //    123, top组件中传递下来的props(center中props声明过的除外)
      this.$emit('getListData', data)
    }
  }
}
//  inheritAttrs:默认值true,继承所有的父组件属性(除props的特定绑定)作为普通的HTML特性应用在
//  子组件的根元素上,如果你不希望组件的根元素继承特性设置inheritAttrs: false,但是class属性会继承
</script>

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值