问题:
起初用Vue.prototype.xxx 方式使用全局变量,但是当遇到页面之间跳出又跳转回来,全局变量存在不及时刷新问题!!!
解决:
采用vuex设置全局变量
新建store/index.js目录

index.js内容
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { / * 是否需要强制登录 */ forcedLogin: false, hasLogin: false, userName: "", totalPrice:0, tagList:[] }, mutations: { update(state,[key,value]){ state[key]=value; }, } }) export default store
main.js内容
import Vue from 'vue' import App from './App' import store from './store' Vue.config.productionTip = false Vue.prototype.$store = store App.mpType = 'app' const app = new Vue({ store, ...App }) app.$mount()
vue页面中取值

computed:{ getTotalPrice(){ return this.$store.state.totalPrice }, getTagList(){ return this.$store.state.tagList } },

vue页面中赋值
const _this = this; _this.$store.commit('update',['totalPrice',500]);
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/201420.html原文链接:https://javaforall.net
