[英]How to access store state from a different store module in vuex?
EDIT : I found the solution which simply was passing the token as an argument in the mounted() function in my vue module:编辑:我发现解决方案只是在我的 vue 模块中将令牌作为参数传递给 mounted() function:
mounted(){
this.$store.dispatch("fetchData", this.$store.state.auth.user.token)
and in the store I use my async function like this:在商店中,我像这样使用我的异步 function:
async fetchData({commit}, id)
I want to make API calls from my store but I also have the user information (userId, jwttoken) necessary for those API calls stored in the same place.我想从我的商店进行 API 调用,但我也有存储在同一个地方的那些 API 调用所需的用户信息(userId,jwttoken)。
this is my store:这是我的商店:
import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
import auth from "./modules/auth";
import prod from "./modules/prod";
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
auth,
prod
},
plugins: [createPersistedState()],
});
this is my authentication module in the store:这是我在商店中的身份验证模块:
import axios from "axios";
const state = {
user: null,
};
const getters = {
isAuthenticated: (state) => !!state.user,
StateUser: (state) => state.user,
};
const actions = {
async Register({ dispatch }, form) {
await axios.post("user/signup", form);
await dispatch("LogIn", form);
},
async LogIn({ commit }, User) {
const response = await axios.post("user/login", User);
console.log(response)
await commit("setUser", response.data);
},
async LogOut({ commit }) {
let user = null;
commit("logout", user);
},
};
const mutations = {
setUser(state, user) {
state.user = user;
},
logout(state) {
state.user = null;
},
};
export default {
state,
getters,
actions,
mutations,
};
and this is where I want to call it in a different store file, with the line this.$store.state.auth.user.token to get the token necessary for the API call:这就是我想在不同的存储文件中调用它的地方,使用 this.$store.state.auth.user.token 行来获取 API 调用所需的令牌:
//store/modules/prod.js
import axios from "axios";
const state = {
products: null,
};
const actions = {
async fetchData({commit}) {
const headers = {
authorization: "Bearer " + this.$store.state.auth.user.token,
};
let resp = await axios.get("product", {
headers
});
await commit("SET_PRODUCTS", resp.data)
}
};
const mutations = {
SET_PRODUCTS(state, products) {
state.products = products
}
};
export default {
state,
actions,
mutations,
};
which I call in a component like this:我在这样的组件中调用它:
products(){
return this.$store.state.products
}
},
mounted(){
this.$store.dispatch("fetchData")
However, the request from the store never sends to the API because it cannot read the token.但是,来自存储的请求永远不会发送到 API,因为它无法读取令牌。 I get "cannot read undefined (reading state)
我得到“无法读取未定义(读取状态)
I've also tried importing it like this but it says token is undefined:我也尝试过像这样导入它,但它说令牌未定义:
import auth from "../modules/auth"
const headers = {
authorization: "Bearer " + auth.user.token,
};
I've read online that you should access the store from non vue modules through Getters but I couldn't manage to make it work either, would this be the correct way to go though?我在网上读到你应该通过 Getters 从非 vue 模块访问商店,但我也无法让它工作,这是否是 go 的正确方法?
Actions take a context object as parameter, you can use it to access rootState. Actions 将上下文 object 作为参数,您可以使用它来访问 rootState。 Check documentation for other available options.
检查文档以获取其他可用选项。 https://vuex.vuejs.org/api/#actions
https://vuex.vuejs.org/api/#actions
const actions = {
async fetchData({commit, rootState}) {
const headers = {
authorization: "Bearer " + rootState.auth.user.token,
};
let resp = await axios.get("product", {
headers
});
await commit("SET_PRODUCTS", resp.data)
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.