创建我的基本组件时,我在 store 之前调度 store 但 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 而出现错误。