Vue Bus 传值

文章介绍了如何通过创建eventBus.js文件,定义一个bus实例来实现Vue应用中任意两个组件之间的通信。在main.js中引入bus,并在组件内使用$bus的方法如$emit、$on和$off进行事件监听和触发,达到传值目的。在组件生命周期钩子中绑定和解绑事件,确保资源的正确管理。

任意两个组件传值可以Vuex,也可使用bus类
bus类:任意两个组件传值,可创建一个bus类负责事件派发、监听和回调管理

建立eventBus.js

src/assets/js/eventBus.js

const install = (Vue) => {
	const Bus = new Vue({
		methods: {
			on (event, ...args) {
				this.$on(event, ...args);
			},
			emit (event, callback) {
				this.$emit(event, callback);
			},
			off (event, callback) {
				this.$off(event, callback);
			}
		}
	})
	Vue.prototype.$bus = Bus;
}
export default install;

在main.js中引入eventBus

src/main.js

import Vue from 'vue'
import Bus from "@/assets/js/eventBus";
Vue.use(Bus); 

组件中使用

src/xxx/xxx/xxx.vue

  mounted() {
  	this.$bus.emit("sendFn", "传值到其它组件");
    this.$bus.on("getFn", v=>"收到其它组件传值");
  },
  destroyed() {
    this.$bus.off("getFn");
  },
Vue 方法多样,以下是一些常用的方法: ### 全局事件总线 在全局配置中挂载全局事件总线 `$bus`: ```javascript new Vue({ render: h => h(App), beforeCreate() { // 挂载全局事件总线 $bus Vue.prototype.$bus = this; }, }).$mount('#app'); ``` 通过新定义一个 Vue 实例,可创建 `bus.js` 文件作为中转站用于兄弟间等 [^1][^2]。 ### 页面间参数递(eventBus) 在小项目、页面量较少的情况下使用,可在全局定义一个 `eventBus`: ```javascript window.eventBus = new Vue(); ``` 在需要递参数的组件中,使用 `$emit` 发送: ```javascript eventBus.$emit('eventBusName', id); ``` 在需要接受参数的组件中,使用 `$on` 接受,并在 `beforeDestroy()` 中关闭 `eventBus`: ```javascript eventBus.$on('eventBusName', function(val) { console.log(val) }) // 在 beforeDestroy 中关闭 beforeDestroy() { eventBus.$off('eventBusName'); } ``` [^3] ### Vuex 使用 Vuex 进行,示例代码如下: ```javascript import Vue from 'vue' import Vuex from 'vuex' import mutations from './mutations' import actions from './actions' import getters from './getters' Vue.use(Vuex) const state = { order: { } } export default new Vuex.Store({ state, mutations, actions, getters }) ``` [^4] ### 父组件向子组件 父组件代码示例: ```vue <template> <ChildComponent :propName="value" /> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { data() { return { value: 'some value' } }, components: { ChildComponent } }; </script> ``` 子组件接收: ```vue <template> <div>{{ propName }}</div> </template> <script> export default { props: ['propName'] }; </script> ``` ### 子组件向父组件 父组件代码: ```vue <template> <ChildComponent @childEvent="handleChildEmit" /> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { methods: { handleChildEmit(value) { console.log('接收到子组件的数据:', value); // 更新父组件状态等操作 } }, components: { ChildComponent } }; </script> ``` 子组件触发事件: ```vue <template> <button @click="sendData">发送数据</button> </template> <script> export default { methods: { sendData() { this.$emit('childEvent', 'data from child'); } } }; </script> ``` [^5] ### `$refs` 和直接调用方法 父组件通过 `$refs` 调用子组件的方法获取数据: ```vue <template> <ChildComponent ref="childRef" /> <button @click="getDataFromChild">获取子组件数据</button> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { methods: { getDataFromChild() { const data = this.$refs.childRef.getData(); console.log('子组件数据:', data); } }, components: { ChildComponent } }; </script> ``` 子组件定义方法: ```vue <template> <div></div> </template> <script> export default { methods: { getData() { return 'data from child'; } } }; </script> ``` [^5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值