在我的组件中,我有一个mounted钩子,它作为异步请求获取一些数据:
mounted() {
this.fetchData()
}在beforeDestroy上,我从商店中删除数据:
beforeDestroy(){
this.destroyDataInStore()
}只要mounted中的请求在组件开始拆卸之前解析,这些工作就很好。
有什么方法可以推迟beforeDestroy,直到承诺已经解决后,在挂载?
发布于 2018-06-08 14:55:52
你可以保存承诺:
export default {
data () {
return {
thePromise: null
}
},
mounted () {
this.thePromise = this.fetchData()
},
beforeDestroy () {
this.thePromise.then(() => {
this.destroyDataInStore()
})
}
}但是为了确保一切正常运行,我将使用created钩子而不是mounted钩子:
export default {
data () {
return {
thePromise: null
}
},
created () {
this.thePromise = this.fetchData()
},
beforeDestroy () {
this.thePromise.then(() => {
this.destroyDataInStore()
})
}
}https://stackoverflow.com/questions/50763217
复制相似问题