首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >延迟销毁vue生命周期钩子

延迟销毁vue生命周期钩子
EN

Stack Overflow用户
提问于 2018-06-08 14:48:42
回答 1查看 1.3K关注 0票数 2

在我的组件中,我有一个mounted钩子,它作为异步请求获取一些数据:

代码语言:javascript
复制
mounted() {
  this.fetchData()
}

beforeDestroy上,我从商店中删除数据:

代码语言:javascript
复制
beforeDestroy(){
  this.destroyDataInStore()
}

只要mounted中的请求在组件开始拆卸之前解析,这些工作就很好。

有什么方法可以推迟beforeDestroy,直到承诺已经解决后,在挂载?

EN

回答 1

Stack Overflow用户

发布于 2018-06-08 14:55:52

你可以保存承诺:

代码语言:javascript
复制
export default {
    data () {
        return {
            thePromise: null
        }
    },
    mounted () {
        this.thePromise = this.fetchData()
    },
    beforeDestroy () {
        this.thePromise.then(() => {
            this.destroyDataInStore()
        })
    }
}

但是为了确保一切正常运行,我将使用created钩子而不是mounted钩子:

代码语言:javascript
复制
export default {
    data () {
        return {
            thePromise: null
        }
    },
    created () {
        this.thePromise = this.fetchData()
    },
    beforeDestroy () {
        this.thePromise.then(() => {
            this.destroyDataInStore()
        })
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50763217

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档