Vue js 在两个组件之间发送数据 NO props
Vue js send data between two components NO props
你好,我正在尝试找到一种方法,将一些布尔值从组件 A 发送到组件 B,无需相互嵌套,无需 props,仅发送数据,单向绑定。
export default {
data: function {
return {
value1: false,
value2:true
}
}
}
使用 Vuex 引入全局状态可能是解决此问题的最佳方法。
在不向系统引入新东西的情况下,您可以使用事件总线来处理。像这样引入侧通道的东西确实会增加您的应用程序的复杂性,但有时是必要的。
然后在你的组件中你可以像这样使用它们
// eventBus.js
import Vue from 'vue';
export const EventBus = new Vue();
// To setup your component to listen and update based on new value
import { EventBus } from './eventBus';
mounted() {
EventBus.$on('newValue', (val) => this.something = val);
}
// To send out a new value
EventBus.$emit('newValue', 5);
你好,我正在尝试找到一种方法,将一些布尔值从组件 A 发送到组件 B,无需相互嵌套,无需 props,仅发送数据,单向绑定。
export default {
data: function {
return {
value1: false,
value2:true
}
}
}
使用 Vuex 引入全局状态可能是解决此问题的最佳方法。
在不向系统引入新东西的情况下,您可以使用事件总线来处理。像这样引入侧通道的东西确实会增加您的应用程序的复杂性,但有时是必要的。
然后在你的组件中你可以像这样使用它们
// eventBus.js
import Vue from 'vue';
export const EventBus = new Vue();
// To setup your component to listen and update based on new value
import { EventBus } from './eventBus';
mounted() {
EventBus.$on('newValue', (val) => this.something = val);
}
// To send out a new value
EventBus.$emit('newValue', 5);