简体   繁体   English

使用 vuex getter 重新渲染组件

[英]Re-render a component using vuex getters

I'm trying to force a component to re-render.我试图强制一个组件重新渲染。 After reading about this, I decided to use key to force re-render.在阅读了这个之后,我决定使用 key 来强制重新渲染。 My key is a value from the store, and then I commit a mutation where it increments the value of the key.我的键是来自商店的值,然后我提交了一个突变,它增加了键的值。 But my components doesn't re-render.但是我的组件不会重新渲染。 What am I doing wrong?我究竟做错了什么?

My component call:我的组件调用:

<div class="firstTab" v-else-if="activeFormStep === 1 && !isLoading">
   <AgenciesSelectionStep
             ref="agenciesSelectionStep"
            :key="agenciesSelectionStepKey"
    ></AgenciesSelectionStep>
</div>

Getters:吸气剂:

computed: {
            ...mapGetters(['modificationFormType', 'globalLoaderUpMessage', 'globalLoaderDownMessage', 'agenciesSelectionStepKey']),
        }

Store State:商店 State:

const state = {
    agenciesSelectionStepKey: 20,
};
const getters = {
    agenciesSelectionStepKey: state => state.agenciesSelectionStepKey,
};

Store mutation;存储突变;

[CHANGE_COMPONENT_KEY]: (state, payload) => {
        state[payload.componentKeyName] += 1;
}

This a long shot but you could try it.这是一个长镜头,但你可以尝试一下。 I hade a similar problem.我有一个类似的问题。 Are you calling the mutation from an action or directly from the component?您是从动作中调用突变还是直接从组件中调用突变?

const state = {
    SelectionStepKeys: {
        agenciesSelectionStepKey: 20
    },
};

const getters = {
    getSelectionStepKey: state => (key) => {
        return state.SelectionStepKeys[key]
    }
};

Mutation突变

IncrementKey: (state, {key}) => {
    state.SelectionStepKeys[key] += 1;
    state.SelectionStepKeys = {...state.SelectionStepKeys}
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM