首页 > 编程知识 正文

vue实时更新页面数据,vue实现局部刷新

时间:2023-05-06 16:32:41 阅读:243116 作者:4960

问题:

起初用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 = falseVue.prototype.$store = storeApp.mpType = 'app'const app = new Vue({ store, ...App})app.$mount()

vue页面中取值

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

 

 

<input class="uni-input" name="totalPrice" type="number" disabled="disabled" v-model="getTotalPrice" />

vue页面中赋值

const _this = this;_this.$store.commit('update',['totalPrice',500]);

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。