Vuex商店更改未更新组件

时间:2019-04-11 07:51:06

标签: javascript vue.js vuex

我面临一个问题,在更新我的储值isRenewalFlow之后,我的组件没有得到更新。

在调试时,我发现在突变updated更新了存储值之后,没有调用组件的[MUTATION_TYPES.IS_RENEWAL_FLOW]生命周期。甚至在商店吸气剂中的getIsRenewalFlow在开始时仅被调用一次,而在突变内部商店更新之后则不会被调用。

这是代码。

  

商店

const state = {
    isRenewalFlow: undefined,
}

const getters = {
    getIsRenewalFlow: (state) => state.isRenewalFlow,
}

const actions = {
    [ACTION_TYPES.POPULATE_QUOTE_DATA]: (
        { dispatch, commit, state, rootState, getters }, payload
    ) => {
            commit(MUTATION_TYPES.IS_RENEWAL_FLOW, payload)
         }
}

const mutations = {
    [MUTATION_TYPES.IS_RENEWAL_FLOW]: (state, payload) => {
        state.isRenewalFlow = payload
    },
}
  

组件

export default Vue.extend({
    components: {
        ErrorBoundary: () => import('@/components/common/ErrorBoundary.vue'),
    },
    computed: {
        ...mapGetters({
            getIsRenewalFlow: 'getIsRenewalFlow',
        }),
   },
    mounted() {
       this.getIsRenewalFlow !== undefined &&
        someFunc()
    },
    updated() {
        this.getIsRenewalFlow !== undefined &&
        someFunc()
    },
})

someFunc从未被调用,即使在state.isRenewalFlow true内用falsemutations更新了[MUTATION_TYPES.IS_RENEWAL_FLOW]

1 个答案:

答案 0 :(得分:1)

updated挂钩与DOM中的更改有关,如果此状态未更新DOM,则不会看到updated被调用。

来自https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram

enter image description here