在创建我的基本组件时,我调度存储,但是在存储之前调度apollo查询init查询,但查询不正确。
我的商店的代码:
import Vuex from 'vuex';
import reportFilter from '../../report-filter/store';
import account from './account-store';
const store = new Vuex.Store({
strict: process.env.NODE_ENV === 'development',
state: {
role: null,
id: null,
},
mutations: {
setAccountData(state, { accountData }) {
state.role = accountData.role;
state.id = accountData.id;
},
},
actions: {
initialize({ commit }) {
// TODO: rewrite after GraphQL backend will be released
commit('setAccountData', { accountData: document.accountData
});
},
},
});
export default store;
主要组件中提供了我的apollo客户端。
new Vue({
router,
store,
svgxuse,
provide: apolloProvider.provide(),
render: h => h(RootLayout),
mounted() {
redirectIfNeeded(this);
},
}).$mount('#app');
BaseLayout组件:
import YNavigation from '../../navigation/Navigation.vue';
import YHeader from '../../header/Header.vue';
import YFooter from '../../footer/Footer.vue';
export default {
data() {
return {};
},
created() {
this.$store.dispatch('account/initialize');
},
components: { YNavigation, YHeader, YFooter },
};
在创建之前,我试图在主要的vue组件中调度存储,但没有帮助。
apollo: {
data: {
query: gql`
query GetDataById($id: ID!) {
${this.$store.state.account.role} {
getDataById(id: $id) {
id
someId
name
}
}
}
`,
update: data => cloneDeep(data.user.getDataById),
variables() {
return {
id: this.actionData.dataId
};
},
skip() {
return this.actionData.dataId === undefined;
},
},
}
我希望通过某些特定角色获取数据,但会出错,因为${this.$store.state.account.role}
在查询字符串中返回null。
答案 0 :(得分:0)
我通过将init存储为插件函数来解决了这个问题
import Vuex from 'vuex';
import reportFilter from '../../report-filter/store';
import account from './account-store';
import user from './user-store';
// called when the store is initialized
const initPlugin = store => {
store.dispatch('account/initialize');
store.dispatch('user/initialize');
};
const store = new Vuex.Store({
strict: process.env.NODE_ENV === 'development',
state: {
// Global store
// This is place for current user data or other global info
},
modules: {
reportFilter,
account,
user,
},
plugins: [initPlugin],
});
export default store;