最近在看vue2.0,感觉就一个字——坑!大大的坑!在做兄弟组件通信的时候$dispatch已经被废了啊。网上资料也都说了各种方法,也就几句代码的事,说起来简单。具体怎么用也没有详细的讲。以下是我实现的效果

import Vue from 'vue';
export default new Vue();

//定义公共文件

·······································································································································································

import bus from 'components/public.js'   //引用相同文件

 methods: {
      addCart (event) {
        if (!event._constructed) {
          return;
        };
        if (!this.food.count) {
          Vue.set(this.food, 'count', 1);
        } else {
          this.food.count++;
        };
        bus.$emit('caradd', event.target);

//A组件派发任务传递数据

      },
      decCart (event) {
        if (!event._constructed) {
          return;
        };
        if (this.food.count) {
          this.food.count--;
        };
      }
    }

·····························································································································································

import bus from 'components/public.js'    //引用相同文件

created () {
      bus.$on('caradd', (res) => {
        console.log(res);
      });
 }

//B组件接收数据,控制台就能输出了


贴上来给大家看怎么传递数据的方法,其他代码可以略过。

Logo

前往低代码交流专区

更多推荐