【发布时间】:2021-02-06 19:37:58
【问题描述】:
我有以下事件发送到父组件并且工作正常:
this.$emit('sendToParent1', true);
this.$emit('sendToParent2');
this.$emit('sendToParent3');
this.$emit('sendToParent4', true);
this.$emit('sendToParent5', false);
但是,我遇到了一个特定问题,需要我按顺序发出这 5 个事件,但要一个接一个。
//emit this first and wait until the parent has finished what its supposed to do
this.$emit('sendToParent1');
//after the parent is done, we emit the next one from the child
this.$emit('sendToParent2');
//same behavior, once it is emitted to parent and the parent finishes executing, then we emit the next one...
this.$emit('sendToParent3');
//again
this.$emit('sendToParent4', true);
//and again
this.$emit('sendToParent5', false);
我相信最好的方法是让一个 prop 检测事件何时完成执行它应该做的任何事情,然后向孩子发送一个 prop,然后当该 prop 更新时,下一个 emit 会被执行,依此类推等等。有人可以指出如何解决这个问题的最佳方向吗?
Parent 具有以下组件:
<component v-on:sendToParent5="parentFunctionFive" v-on:sendToParent4="parentFunctionFour" v-on:sendToParent1="parentFunctionOne" v-on:sendToParent2="parentFunctionTwo" v-on:sendToParent3="parentFunctionThree"></component>
Parent 有以下方法:
parentFunctionOne: function() {
axios.get('/api/do_something-1/')
.then((res)=>{
this.something1 = res.data;
})
.catch((error)=>{
console.log(error);
});
},
parentFunctionTwo: function() {
axios.get('/api/do_something-2/')
.then((res)=>{
this.something2 = res.data;
})
.catch((error)=>{
console.log(error);
});
},
parentFunctionThree: function() {
axios.get('/api/do_something-3/')
.then((res)=>{
this.something3 = res.data;
})
.catch((error)=>{
console.log(error);
});
},
parentFunctionFour: function(passed_value) {
//changing the value of a variable in data
this.trueOrFalse = passed_value;
},
parentFunctionFive: function(passed_value) {
//changing the value of a variable in data
this.valuePassed = passed_value;
}
【问题讨论】:
-
在相关说明中,Vue 2 文档推荐 always using kebab case for event names
标签: javascript vue.js emit prop