Vuex 突变使用来自 async/await 操作的动态 属性 名称更新对象
Vuex mutations updating an object with dynamic property names from async/await action
所以我试图让 Nuxt 查询我的 GQL API 以获取站点菜单。我通过我的 index.js
商店模块中的 nuxtServerInit
函数来执行此操作。
像这样:
menuLocations = ["MAIN_MENU", "WORK_MENU"]
store.dispatch("menus/QUERY_MENUS", menuLocations)
从我的 menus.js
商店模块调用我的 QUERY_MENUS
操作。代码是这样的:
// Define State defaults
export const state = () => ({
locations: {}
})
// Define mutations
export const mutations = {
SET_MENU(state, data) {
//Vue.set(state.locations, data.location, data.items)
//state.locations = { ...state.locations, [data.location]: data.items }
}
}
// Define actions
export const actions = {
async QUERY_MENUS({ commit }, menuLocations) {
let client = this.app.apolloProvider.defaultClient
// Get each menu from server
for (const location of menuLocations) {
const query = await client.query({
query: MenuByLocation,
variables: {
location: location
}
})
// Commit menu to store
commit("SET_MENU", {
location: _camelCase(location),
items: _get(query, "data.menuItems.nodes", {})
})
}
}
}
问题是 SET_MENU
中注释掉的两行都不能可靠地工作(当未注释时),有时它们工作,有时他们不工作。我猜这与 Nuxt 和 SSR 有关,或者我做错了 async/await 事情?
在此处编写沙盒代码:
代码:https://codesandbox.io/s/j3yjz2wm6y?fontsize=14
预览:https://j3yjz2wm6y.sse.codesandbox.io/
有什么建议吗?谢谢!
在提供的沙箱中有两个错误:
1) nuxtServerInit中没有等待调度
export const actions = {
async nuxtServerInit(store, context) {
let menuLocations = ["MAIN_MENU", "OUR_WORK_MENU"];
await store.dispatch("menus/QUERY_MENUS", menuLocations);
}
};
2) 在计算中 属性 缺少状态引用
return _get(this, "$store.state.menus.locations.mainMenu", false);
这里修复了 CBS -> https://codesandbox.io/s/rrrv17oq8m
所以我试图让 Nuxt 查询我的 GQL API 以获取站点菜单。我通过我的 index.js
商店模块中的 nuxtServerInit
函数来执行此操作。
像这样:
menuLocations = ["MAIN_MENU", "WORK_MENU"]
store.dispatch("menus/QUERY_MENUS", menuLocations)
从我的 menus.js
商店模块调用我的 QUERY_MENUS
操作。代码是这样的:
// Define State defaults
export const state = () => ({
locations: {}
})
// Define mutations
export const mutations = {
SET_MENU(state, data) {
//Vue.set(state.locations, data.location, data.items)
//state.locations = { ...state.locations, [data.location]: data.items }
}
}
// Define actions
export const actions = {
async QUERY_MENUS({ commit }, menuLocations) {
let client = this.app.apolloProvider.defaultClient
// Get each menu from server
for (const location of menuLocations) {
const query = await client.query({
query: MenuByLocation,
variables: {
location: location
}
})
// Commit menu to store
commit("SET_MENU", {
location: _camelCase(location),
items: _get(query, "data.menuItems.nodes", {})
})
}
}
}
问题是 SET_MENU
中注释掉的两行都不能可靠地工作(当未注释时),有时它们工作,有时他们不工作。我猜这与 Nuxt 和 SSR 有关,或者我做错了 async/await 事情?
在此处编写沙盒代码:
代码:https://codesandbox.io/s/j3yjz2wm6y?fontsize=14
预览:https://j3yjz2wm6y.sse.codesandbox.io/
有什么建议吗?谢谢!
在提供的沙箱中有两个错误:
1) nuxtServerInit中没有等待调度
export const actions = {
async nuxtServerInit(store, context) {
let menuLocations = ["MAIN_MENU", "OUR_WORK_MENU"];
await store.dispatch("menus/QUERY_MENUS", menuLocations);
}
};
2) 在计算中 属性 缺少状态引用
return _get(this, "$store.state.menus.locations.mainMenu", false);
这里修复了 CBS -> https://codesandbox.io/s/rrrv17oq8m