在项目里使用了vuex管理状态中碰到一个问题,在获取同一个变量的时候只有提交变量的页面有值,其它页面获取不到,见附件。
index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
.....
const store = new Vuex.Store({
state: {
chatList: [],
},
mutations: {
setChatList: function(state, chatList) {
state.chatList = chatList;
},
main.js
import Vue from 'vue'
import store from './store'
import App from './App'
.....
Vue.prototype.$store = store;
const app = new Vue({
store,
...App
})
提交变量且能正常获取到值的页面
//提交数据
self.$store.commit("setChatList", chatList);
//获取数据
getChat:function(e){
let self=this;
console.log("获取chatList")
console.log(self.$store.state.chatList);
},
不能正常获取到值的页面
getChat:function(e){
let self=this;
console.log("获取chatList")
console.log(self.$store.state.chatList);
},