vue2.0子组件向父组件传递数据(饿了么例子)vue2.0废弃$dispatch,替换成$emit 和$refs

本文介绍Vue2.0中子组件如何通过$emit向父组件传递事件,并演示具体的实现方式,包括如何监听事件及调用其他组件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue2.0废弃$dispatch,替换成$emit 和$refs,使用方法如下(根据慕课饿了么案例,教程上的写法要改[为了购物车小球抛下的动画]):

// goods.vue组件--使用v-on监听
<div class="cartcontrol-wrapper">
  <cartcontrol :food="food" v-on:cart-add="cartAdd"></cartcontrol>
</div>
在methods添加cartAdd函数

// 监听v-on:cart-add="cartAdd",购物车组件如果提交了'cart-add'事件就调用这个cartAdd函数,
// 对应cartcontrol.vue中methods里面addNum()函数里的
this.$emit('cart-add', event.target);
这句提交名为'cart-add'的事件给父组价.
methods: {

  cartAdd(el) {
          // dom元素更新后执行, 因此此处能正确打印出更改之后的值;
 this.$nextTick(() => {
 this.$refs['shopcart'].drop(el); //调用shopcart组件的drop()函数
});
}
}

1
2
good.vue  // v-el的写法在vue2.0中已经废弃,现在全部采用 ref和$ref

定Dom节点元素
<shopcart ref="shopcart" :select-foods="selectFoods" :delivery-price="seller.deliveryPrice" :min-price="seller.minPrice"></shopcart>
1
2
3
4
5
6
7
8
9
10
11
// cartcontrol.vue组件
addCart(event) {
  if (!event._constructed) {
    return;
  }
  if (!this.food.count) {
     Vue.set(this.food, 'count', 1);
  else {
      this.food.count++;
  }
  this.$emit('cart-add', event.target); //添加这句,提交'cart-add'事情给父组件,第二个是要传递的参数
},// cartcontrol.vue组件
1
2
3
4
5
// shopcart.vue组件的drop()函数
methods: {
    drop(el) {
        console.log(el);
    }
}// shopcart.vue组件

子组件向父组件传递过程梳理:

目标:把自组件cartcontrol的addCart()里用this.$emit('cart-add',event.target)这句,获取并把添加按钮这个DOM元素以'cart-add'名字传递给父组件good.vue,

传给父组件good.vue,父组件里面的

<cartcontrol :food="food"  v-on:cart-add="cartAdd"></cartcontrol>用v-on:cart-add="cartAdd" 表示监听传来的cart-add事件,监听到就调用cartAdd()函数处理,

cartAdd()函数里这句this.$refs['shopcart'].drop(el);调用shopcart组件的drop()函数

shopcart.vue组件的函数是打印出当前参数.

Vue 2中,孙子组件想要获取爷爷组件的`$refs`引用通常需要通过事件总线或者Vuex状态管理工具,因为直接父子、祖孙之间的数据传递通常是单向数据流,避免了深度嵌套的`this.$parent`或`this.$grandparent`。 1. **事件总线**:你可以创建一个全局的事件中心,当爷爷组件设置`ref`并触发一个事件时,孙子组件可以监听这个事件来获取`$refs`的相关信息。 ```javascript // 爷爷组件 <template> <button @click="emitGrandparentRef">点击获取ref</button> <Child ref="childRef" /> </template> <script> export default { methods: { emitGrandparentRef() { this.$emit('get-grandparent-ref', this.childRef); } } }; </script> // 孙子组件 <template> <div>{{ grandparentRef }}</div> </template> <script> import bus from '@/events/bus'; // 假设你有一个全局事件中心 export default { mounted() { bus.on('get-grandparent-ref', (grandparentRef) => { this.grandparentRef = grandfatherRef; }); }, beforeDestroy() { bus.off('get-grandparent-ref'); } }; </script> ``` 2. **Vuex**:如果数据需要在整个应用共享,也可以考虑使用Vuex来存储获取`$refs`。爷爷组件将`ref`值保存在store中,孙子组件订阅这个数据即可。 ```javascript // store.js state: { childRef: null, }, actions: { setGrandparentRef({ commit }, ref) { commit('SET_CHILD_REF', ref); } }, mutations: { SET_CHILD_REF(state, ref) { state.childRef = ref; } } // 爷爷组件 methods: { setChildRef() { this.$refs.childRef && this.$store.dispatch('setGrandparentRef', this.$refs.childRef); } } // 孙子组件 computed: { grandparentRef() { return this.$store.state.childRef; } } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值