简体   繁体   English

Vuex:从动作中调用 getter

[英]Vuex: Call getters from action

Is there a way for a dispatch/action to call a getter inside of it?有没有办法让调度/动作在其中调用吸气剂?

mutations: {
    setData(state, data) {
        state.data = data;
    }
}
actions: {
    sendDataToServer({ commit }, payload) {
        // call getter (data) and assign to variable
        // do async functions from the data returned
    }
},
getters: {
    getAppData: state => () => {
        return state.data;
    }
}

So what's the best practice here?那么这里的最佳实践是什么? Using the mutation to change the state and then get the state and pass it to action which will then execute the async function or do I need to restructure my implementation?使用突变来改变状态,然后获取状态并将其传递给动作,然后执行异步函数还是我需要重构我的实现?

call mutation -> get the data via getter -> call action调用变异 -> 通过 getter 获取数据 -> 调用动作

OR或者

do it all on the action (do mutation on the action and do the action/async method without the need of the getter)?在动作上做这一切(在动作上做变异并在不需要 getter 的情况下执行动作/异步方法)?

Is there a way for a dispatch/action to call a getter inside of it?有没有一种方法可以让派遣/操作在其中调用吸气剂?

mutations: {
    setData(state, data) {
        state.data = data;
    }
}
actions: {
    sendDataToServer({ commit }, payload) {
        // call getter (data) and assign to variable
        // do async functions from the data returned
    }
},
getters: {
    getAppData: state => () => {
        return state.data;
    }
}

So what's the best practice here?那么,这里的最佳实践是什么? Using the mutation to change the state and then get the state and pass it to action which will then execute the async function or do I need to restructure my implementation?使用变异来更改状态,然后获取状态并将其传递给操作,该操作将执行异步功能,还是我需要重组实现?

call mutation -> get the data via getter -> call action呼叫变异->通过getter获取数据->呼叫操作

OR或者

do it all on the action (do mutation on the action and do the action/async method without the need of the getter)?是否在动作上全部完成(在动作上进行突变以及不需要getter的动作/异步方法)?

Is there a way for a dispatch/action to call a getter inside of it?有没有一种方法可以让派遣/操作在其中调用吸气剂?

mutations: {
    setData(state, data) {
        state.data = data;
    }
}
actions: {
    sendDataToServer({ commit }, payload) {
        // call getter (data) and assign to variable
        // do async functions from the data returned
    }
},
getters: {
    getAppData: state => () => {
        return state.data;
    }
}

So what's the best practice here?那么,这里的最佳实践是什么? Using the mutation to change the state and then get the state and pass it to action which will then execute the async function or do I need to restructure my implementation?使用变异来更改状态,然后获取状态并将其传递给操作,该操作将执行异步功能,还是我需要重组实现?

call mutation -> get the data via getter -> call action呼叫变异->通过getter获取数据->呼叫操作

OR或者

do it all on the action (do mutation on the action and do the action/async method without the need of the getter)?是否在动作上全部完成(在动作上进行突变以及不需要getter的动作/异步方法)?

Is there a way for a dispatch/action to call a getter inside of it?有没有一种方法可以让派遣/操作在其中调用吸气剂?

mutations: {
    setData(state, data) {
        state.data = data;
    }
}
actions: {
    sendDataToServer({ commit }, payload) {
        // call getter (data) and assign to variable
        // do async functions from the data returned
    }
},
getters: {
    getAppData: state => () => {
        return state.data;
    }
}

So what's the best practice here?那么,这里的最佳实践是什么? Using the mutation to change the state and then get the state and pass it to action which will then execute the async function or do I need to restructure my implementation?使用变异来更改状态,然后获取状态并将其传递给操作,该操作将执行异步功能,还是我需要重组实现?

call mutation -> get the data via getter -> call action呼叫变异->通过getter获取数据->呼叫操作

OR或者

do it all on the action (do mutation on the action and do the action/async method without the need of the getter)?是否在动作上全部完成(在动作上进行突变以及不需要getter的动作/异步方法)?

Is there a way for a dispatch/action to call a getter inside of it?有没有一种方法可以让派遣/操作在其中调用吸气剂?

mutations: {
    setData(state, data) {
        state.data = data;
    }
}
actions: {
    sendDataToServer({ commit }, payload) {
        // call getter (data) and assign to variable
        // do async functions from the data returned
    }
},
getters: {
    getAppData: state => () => {
        return state.data;
    }
}

So what's the best practice here?那么,这里的最佳实践是什么? Using the mutation to change the state and then get the state and pass it to action which will then execute the async function or do I need to restructure my implementation?使用变异来更改状态,然后获取状态并将其传递给操作,该操作将执行异步功能,还是我需要重组实现?

call mutation -> get the data via getter -> call action呼叫变异->通过getter获取数据->呼叫操作

OR或者

do it all on the action (do mutation on the action and do the action/async method without the need of the getter)?是否在动作上全部完成(在动作上进行突变以及不需要getter的动作/异步方法)?

Action handlers receive a context object which exposes the same set of methods/properties on the store instance, so you can call context.commit to commit a mutation, or access the state and getters via context.state and context.getters操作处理程序接收一个上下文对象,该对象在存储实例上公开相同的一组方法/属性,因此您可以调用 context.commit 来提交更改,或通过 context.state 和 context.getters 访问状态和 getter

   actions: {
            sendDataToServer(context, payload) {
                // context object contains state, commit, getters
                context.getters.getAppData
            }
        },

Refer docs: https://vuex.vuejs.org/guide/actions.html#dispatching-actions参考文档: https : //vuex.vuejs.org/guide/actions.html#dispatching-actions

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

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