Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 我可以在getter函数中分派vuex操作吗?_Vue.js_Vuex - Fatal编程技术网

Vue.js 我可以在getter函数中分派vuex操作吗?

Vue.js 我可以在getter函数中分派vuex操作吗?,vue.js,vuex,Vue.js,Vuex,我想在getter函数中分派操作。 1.这是可能的,也是正确的。 2.如果是,我怎么做 我猜这会是类似于这篇快讯(“GET_BOOKS”) 但它不起作用。 所以我的商店文件看起来像这样 const initialState = { books: null }; const getters = { getAllBooksDispatch: (state, getters, dispatch) => { if (state.books === null)

我想在getter函数中分派操作。
1.这是可能的,也是正确的。
2.如果是,我怎么做

我猜这会是类似于这篇快讯(“GET_BOOKS”)

但它不起作用。
所以我的商店文件看起来像这样

const initialState = {
    books: null
};

const getters = {
    getAllBooksDispatch: (state, getters, dispatch) => {
        if (state.books === null) {
            dispatch('GET_BOOKS');
        }
        return state.books
    },
};

const mutations = {
    SET_BOOKS: (state,{data}) => {
        console.log('SET_BOOKS mutations')
        state.books = data;
    },
};

const actions = {
    GET_BOOKS: async ({ commit }) => {
        let token = users.getters.getToken;

        let query = new Promise((resolve, reject) => {
            axios.get(config.api + 'books', {token}).then(({data}) => {
                if (data) {
                    commit('SET_BOOKS', {data: data})
                    resolve()
                } else {
                    reject(data.message);
                }
            }).catch(() => {
                reject('Error sending request to server!');
            })
        })

    },
};

不,你不能。至少不是你想要的方式。getter中的第三个参数是使用模块时的
rootState
对象,而不是
dispatch
。即使您找到了在getter中分派操作的方法,它也不会按照您期望的方式工作。getter必须是同步的,但操作可以是(在本例中也是)异步的。在您的示例中,
GET\u BOOKS
将被调度,但getter仍将
state.BOOKS
返回为null

我建议在Vuex商店外处理这种延迟加载。

请详细说明“但它不起作用”。
const initialState = {
    books: null
};

const getters = {
    getAllBooksDispatch: (state, getters, dispatch) => {
        if (state.books === null) {
            dispatch('GET_BOOKS');
        }
        return state.books
    },
};

const mutations = {
    SET_BOOKS: (state,{data}) => {
        console.log('SET_BOOKS mutations')
        state.books = data;
    },
};

const actions = {
    GET_BOOKS: async ({ commit }) => {
        let token = users.getters.getToken;

        let query = new Promise((resolve, reject) => {
            axios.get(config.api + 'books', {token}).then(({data}) => {
                if (data) {
                    commit('SET_BOOKS', {data: data})
                    resolve()
                } else {
                    reject(data.message);
                }
            }).catch(() => {
                reject('Error sending request to server!');
            })
        })

    },
};