Javascript 将对象提交到vuex中的状态
我试图通过Javascript 将对象提交到vuex中的状态,javascript,ecmascript-6,vue.js,axios,vuex,Javascript,Ecmascript 6,Vue.js,Axios,Vuex,我试图通过$store.commit('fetchFunction',response.data)从axios response获取对象。我需要在App.vue(根组件)中计算出以在SPA中“全局”存储阵列(vue路由器)。 这就是我得到的 auth.js store.js App.vue 我的实际问题是,如果我更改路由或重新加载页面,对象将成为观察者,而store不会存储它。 我以前几乎有同样的功能,只是true和false(我用JWT令牌处理身份验证),它就像一个charme。 我的问题是以
$store.commit('fetchFunction',response.data)
从axios response获取对象。我需要在App.vue(根组件)中计算出
以在SPA中“全局”存储阵列(vue路由器
)。
这就是我得到的
auth.js
store.js
App.vue
我的实际问题是,如果我更改路由或重新加载页面,对象将成为观察者,而store
不会存储它。
我以前几乎有同样的功能,只是true
和false
(我用JWT令牌处理身份验证),它就像一个charme。
我的问题是以良好的方式传递对象。用于通过路由更改跟踪更改 使用会话代理解决方案!这是我喜欢的一种方式,它的工作真的很好!谢谢
axios.get('/api/to/userController/id')
.then(response => {
//this is a nice json
console.log(response.data.data);
app.$store.commit('userProfile', response.data.data)
})
.catch(error => {
console.log(error);
});
state: {
userProfile: {}
},
mutations: {
userProfile (state, payload) {
state.userProfile = payload;
}
},
getters: {
userProfile: state => {
//this is [__ob__: Observer] lenght: 0
console.log(state.userProfile)}
return state.userProfile;
}
created() {
//this is filled with the observer from store.js - Until reload page!
//after reload page - it is null!
console.log();
},
computed: {
userProfile() {
return this.$store.getters.UserProfile;
}
}