我有以下方法:
getData(id){
this.$store.dispatch('getData', {
employeeId: this.employeeId
}).then(() => {
if (this.employeeData.length) {
// here some code...
}
});
},
selectEmployee(d) {
this.getData(d.employeeId);
// I need to execute this only after getData has been processed...
this.$store.dispatch('fetchHistory');
},
所以我想做的是这样的:
selectEmployee(d) {
this.getData(d.employeeId).then(() => {
// check data here and then execute the fetch
this.$store.dispatch('fetchHistory');
});
},
但我在then
安全中得到错误,似乎那里是不允许的。
有什么线索吗?
发布于 2018-07-25 03:24:38
在getData
中返回promise,然后你就可以做getData().then()
了
getData(id){
return this.$store.dispatch('getData', {
employeeId: this.employeeId
}).then(() => {
if (this.employeeData.length) {
// here some code...
}
});
}
发布于 2018-07-25 03:34:10
您还可以使用ES6s async await,而不是链接Promise,这使得代码更简洁、更具可读性。
async getData(id) {
await this.$store.dispatch('getData', {
employeeId: id,
});
if(this.employeeDate) //do something
},
async selectEmployee(d) {
await this.getData(d.employeeId);
// I need to execute this only after getData has been processed...
this.$store.dispatch('fetchHistory');
},
https://stackoverflow.com/questions/51506116
复制相似问题