这有效:
fetchProjects({ commit }) {
axios.get('http://localhost:5000/projects').then(response => {
commit('SET_PROJECTS', response.data);
});
这不是:
fetchProjects({ commit }) {
ProjectService.getProjects().then(response => {
commit('SET_PROJECTS', response.data);
});
这是ProjectService
const apiClient = axios.create({
baseURL: 'http://localhost:5000/api/v1',
withCredentials: false,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
});
export default {
getProjects() {
return apiClient.get('/projects');
}
}
在Vuex中:当我直接使用axios时,没有问题。但是,当我点击我的ProjectService时,它没有向服务器发送身份验证信息(承载者令牌),并返回401错误。直接从组件点击ProjectService效果很好,但在使用Vuex时效果不佳。
可能导致这种情况发生的原因。如何使用服务代替基本的axios呼叫?