嗨,我对vm.$dispatch
有点困惑,因为我希望它从侦听器的回调中返回值以获得进一步的逻辑,而这些逻辑无法放在回调 _(imo)_ 中,但结果证明它是实例它自己。 我正在学习 Vue,可能错过了一些东西。
var vm = new Vue({
// omitted
events: {
'object:creating' : function (args) {
// omitted
return false;
},
'object:created' : function (args) {
// omitted
}
}
});
var child = new Vue({
parent: vm,
// omitted
methods: {
performWhatever: function (args) {
//
},
createObject: function (args) {
// Pre-create
if ( this.$dispatch('object:creating', args) === false ) {
// Stop the creation
}
// The business logic
this.performWhatever(args);
// Post-create
this.$dispatch('object:created', args);
}
}
})
如您所见,预创建操作是可选的。
谢谢你。
SomeAsyncCode === false
$dispatch
期望您传递多个参数,包括回调函数。this.$dispatch('object:creating', args, function() {
// The business logic
this.performWhatever(args);
})
在接收方一侧,
events: {
'object:creating' : function (args, callback) {
// Do something with args and fire callback (or not)
callback()
}
}
感谢@fnlctrl的解释:)
@fnlctrl谢谢你的解释。 是异步的,我忘了,多尴尬=D
最有用的评论
SomeAsyncCode === false
$dispatch
期望您传递多个参数,包括回调函数。在接收方一侧,