美文网首页
vue组件间传值之eventBus

vue组件间传值之eventBus

作者: spencer_sun | 来源:发表于2020-05-21 14:18 被阅读0次

1 概述:

vue组件间的传值,父子之间props 和emit; 跨组件间可以使用vuex或者eventBus;

eventBus 即const bus = new Vue(); 通过 vue的emit 和on 触发和监听,进行值的传递;

2 Demo

项目结构

- test1. vue
- test2.vue
- parent.vue
- eventBus.js

代码示例

*eventBus.js

import Vue from 'vue'
let Bus = new Vue({
    data: function () {
        return {
            a: 1,
        }
    }
});
export { Bus }

*test1.vue

<template>
    <div>
        <p>test1</p>
        <div>{{testA}}</div>
        <button @click="handleClick">clickA</button>
    </div>
</template>

<script>
import { Bus } from "./../eventBus";

export default {
    name: "testA",
    data: function() {
        return {
            testA: 1
        };
    },
    methods: {
        handleClick() {
            this.testA++;
            Bus.$emit("handleTestA", this.testA);
        }
    }
};
</script>

  • test2.vue
<template>
    <div>
        <p>test2</p>
        <div>{{testB}}</div>
        <button @click="handleClick">clickB</button>
    </div>
</template>

<script>
import { Bus } from "./../eventBus";

export default {
    name: "testB",
    data: function() {
        return {
            testB: 1
        };
    },
    methods: {
        handleClick() {
            this.testB++;
            Bus.$emit("handleTestB", this.testB);
        }
    }
};
</script>

*parent.vue

<template>
    <div>
        <p>parent</p>
        <testA></testA>
        <testB></testB>
        <div>
            <h3>testA{{receivedTestA}}</h3>
            <h3>testB{{receivedTestB}}</h3>
        </div>
    </div>
</template>

<script>
import { Bus } from "./../eventBus";
import testA from "./test1";
import testB from "./test2";
export default {
    name: "aaa",
    data: function() {
        return {
            receivedTestA: "",
            receivedTestB: ""
        };
    },
    components: {
        testA,
        testB
    },
    mounted: function() {
        Bus.$on("handleTestB", val => {
            this.receivedTestA = val;
        });
        Bus.$on("handleTestA", val => {
            this.receivedTestB = val;
        });
    },
    methods: {}
};
</script>

3 结果示例

Screen Shot 2020-05-21 at 2.17.01 PM.png

相关文章

  • vue组件间传值之eventBus

    1 概述: vue组件间的传值,父子之间props 和emit; 跨组件间可以使用vuex或者eventBus; ...

  • vue 6种通信总结②

    继续上一篇的vue 6种通信总结① 非组件间的组件间的传值(简称:EventBus) 我先举个例子来解释下组件间的...

  • 前端基础搬运工-VUE模块

    十、VUE模块 基础部分 1. Vue组件间传值 答: -[ ] 1.父子之间的传值 父组件向子组件传值通过p...

  • eventBus传值、js或html调用vue方法、父子方法调用

    使用eventBus传值 新建eventBus.jsimport Vue from 'vue'// 用于监听、触发...

  • vue 6种通信总结①

    常用vue通信大概有这几种方式进行: 组件间的父子之间的传值 组件间的子父之间的传值 非组件间的组件间的传值(简称...

  • Vue和React组件通信的总结

    在现代的三大框架中,其中两个Vue和React框架,组件间传值方式有哪些? 组件间的传值方式 组件的传值场景无外乎...

  • Vue组件间通信

    Vue组件间通信 父子组间通信 通过props向子组件传值,通过事件向父级组件发送消息 通过props向子组件传值...

  • vue通信方法总结(未)

    1. 父给子通信 2. 子向父传值 3. 兄弟组件传值 eventbus 专门用一个空的 Vue 实例(Bus)来...

  • 2020-03月前端面试题

    vue相关 vue父子组件传值方式有哪些? 兄弟组件间如何传值? vuex是用来干什么的? vuex核心模块有哪些...

  • vue 父子组件间的传值

    vue 中为了避免重复的代码,使页面更简洁,经常使用到组件,使用组件会牵扯到组件间的传值 常用的传值有: 父子间的...

网友评论

      本文标题:vue组件间传值之eventBus

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