我有两个Vue组件:
Vue.component('A', {});
Vue.component('B', {});
我如何从组件B访问组件A?这些组件之间的通信如何工作?
我有两个Vue组件:
Vue.component('A', {});
Vue.component('B', {});
我如何从组件B访问组件A?这些组件之间的通信如何工作?
var parent = new Vue({
template: '<div><child></child></div>',
created: function () {
this.$on('child-created', function (child) {
console.log('new child created: ')
console.log(child)
})
},
components: {
child: {
created: function () {
this.$dispatch('child-created', this)
}
}
}
}).$mount()
Dan Holloran最近在这篇 文章中谈到了他与跨组件消息传递的“斗争”,如果您需要在没有父子关系的组件之间进行通信,这可能对您有所帮助。
除了使用事件进行通信之外,我还有一种经验丰富的方法,即使用一个中央组件注册表,其中包含对公共API的引用,该API实例绑定到一个组件。注册表处理对组件的请求并返回其公共API。
在Vue.js的上下文中,事件是我的首选。
$dispatch
和 $broadcast
已经被废弃。 - hitautodestructvar hub = {
state: {
message: 'Hello!'
}
}
var vmA = new Vue({
el: '#appA',
data: {
pState: {
dA: "hello A"
},
hubState: hub.state
}
})
var vmB = new Vue({
el: '#appB',
data: {
pState: {
dB: "hello B"
},
hubState: hub.state
}
})
在您的Vue应用程序中,还可以通过创建单个全局事件中心来建立组件之间的通信。像这样:
var bus = new Vue();
现在,您可以创建自定义事件并从任何组件监听它们。
// A.vue
// ...
doThis() {
// do the job
bus.$emit('done-this');
}
// B.vue
// ...
method:{
foo: function()
}
created() {
bus.$on('done-this', foo);
}
更多相关信息可以在官方文档中找到。
bus
需要以某种方式对所有组件可见。我使用全局 Window
变量(据说这是个坏主意),因为我无法以能够正常工作的方式进行 import
。 - WoJ最佳实践是使用props
和events
。
有许多在线示例,如:
我建议您阅读一些相关主题的内容。
如果组件是同级并且没有父子关系,则值得检查应用程序的架构。
A
和B
是否具有父子关系?C
可能是A
和B
的父组件?如果A
和B
是C
的子级,请考虑使用props
和events
。
另一种方法是使用props
和sync
,这对于表单数据非常有用:
对于兄弟组件之间的通信,我发现使用父组件作为事件总线可以使逻辑相当简单。使用$root
作为事件总线需要额外的代码来检查可能不是直接兄弟的组件范围。而使用$parent
意味着可以控制所发出事件的范围。
以下示例是针对TableHeader
组件的。当单击时,它会重新排序表中的数据,其他标题不再处于活动状态,因此不应显示为这样,计算属性cssClass
用于此目的。
export default {
name: 'TableHeader',
props: ['sort'],
data() {
return {
direction: this.sort
}
},
computed: {
cssClass() {
if (this.direction === 'none') return '';
return (this.direction === 'descending') ? 'headerSortUp': 'headerSortDown';
}
},
methods: {
clickHeader(event) {
this.direction = (this.direction === 'none' || this.direction === 'descending') ? 'ascending' : 'descending';
//We use the parent as an event bus
this.$parent.$emit('TableHeader:sortChanged', this);
},
sortChanged(element) {
//Dont reset if this is the emitter
if (this === element) return;
this.direction = 'none';
}
},
created() {
this.$parent.$on('TableHeader:sortChanged', this.sortChanged);
},
beforeDestroy: function () {
this.$parent.$off('TableHeader:sortChanged', this.sortChanged)
}
}
在Vuejs组件之间进行通信有很多选项。如果您的组件是父子关系,则应使用“props”将数据从父组件发送到子组件,并使用“events”从子组件向父组件通信。如果您的组件是兄弟关系,则需要使用“store”,否则可以使用“$root”属性。
从父组件到子组件
父组件
<ChildComponent :propsData="dataToPassToChild" />
子组件必须具有属性
props: ['propsData']
子组件到父组件
子组件
this.$emit('messegeToParent', arg1, arg2, ...);
父组件
<ChildComponent @messageToParent="messageReceivedFromChild" />
以下方法应该在子组件中
messageReceivedFromChild(arg1, arg2, ...) {
}
兄弟组件
组件1
this.$root.$emit('message1', arg1, arg2, ...);
组件 2
this.$root.$on('message1', (arg1, arg2, ...) => {
});