自定义全局变量在uniapp的Vuex应用

       本文介绍了uniapp使用自定义全局变量的方法。当同一业务在连续页面操作时,存在部分筛选变量需要始终保持一致,比如时间筛选条件等,来回跳转页面时如果采用变量传递,常较为繁琐,存在遗漏传递或未清除上一次变量值,造成错误。本文介绍了通过store本地缓存机制,实现全局变量的方法。

一、自定义全局变量设置文件businessGlobeVariable.js

@/store/modules/businessGlobeVariable

解释:

state:  自定义变量(本地缓存变量数据)

mutation: 数据修改

action:异步操作,调用mutation里的方法,从而修改state缓存数据

getters: 读取state数据


const businessGlobeVariable = {
    namespaced: true,
    state: {
        // 日常点检
        timeRangeTypeCheckDaily: '',
        // 周期巡视
        timeRangeTypeInspectionDaily: '',
    },
    mutations:{
        SET_TIME_RANGE_TYPE_CHECK_DAILY: (state, timeRangeTypeCheckDaily) => {
            state.timeRangeTypeCheckDaily = timeRangeTypeCheckDaily
        },
        SET_TIME_RANGE_TYPE_INSPECTION_DAILY: (state, timeRangeTypeInspectionDaily) => {
            debugger
            state.timeRangeTypeInspectionDaily = timeRangeTypeInspectionDaily
        },
    },
    actions:{
        SetTimeRangeTypeCheckDaily({ commit }, timeRangeTypeCheckDaily) {
            commit('SET_TIME_RANGE_TYPE_CHECK_DAILY', timeRangeTypeCheckDaily);
        },
        SetTimeRangeTypeInspectionDaily({ commit }, timeRangeTypeInspectionDaily) {
            debugger
            commit('SET_TIME_RANGE_TYPE_INSPECTION_DAILY', timeRangeTypeInspectionDaily);
        },
    },
    getters: {
        timeRangeTypeCheckDaily: state => state.timeRangeTypeCheckDaily,
        timeRangeTypeInspectionDaily: state => state.timeRangeTypeInspectionDaily,
    }
}
export default businessGlobeVariable

二、缓存变量管理文件

@/store/index.js

import businessGlobeVariable from '@/store/modules/businessGlobeVariable'



const store = new Vuex.Store({
  modules: {
    user,
    businessGlobeVariable
  },

......

})

三、使用

(1)设置全局变量

调用mutation方法并传参

this.$store.dispatch('businessGlobeVariable/SetTimeRangeTypeCheckDaily','lastThreeMonths')

(2)读取全局变量

Let e = this.$store.getters['businessGlobeVariable/timeRangeTypeCheckDaily']

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp 中使用 Vuex,可以实现全局状态管理。下面是一个简单的示例,介绍如何在 Uniapp 中使用 Vuex。 1. 首先,在你的项目中安装 Vuex: ```bash npm install vuex ``` 2. 在你的项目中创建一个 `store` 目录,用于存放 Vuex 相关的文件。 3. 在 `store` 目录下创建一个 `index.js` 文件,用于配置 Vuex 的核心内容。 ```javascript // store/index.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment(state) { state.count++ }, decrement(state) { state.count-- } }, actions: { increment(context) { context.commit('increment') }, decrement(context) { context.commit('decrement') } }, getters: { getCount(state) { return state.count } } }) export default store ``` 在上述代码中,我们定义了一个包含 `state`、`mutations`、`actions` 和 `getters` 的 Vuex store。`state` 用于存储应用的状态,`mutations` 用于修改状态,`actions` 用于提交 mutations,`getters` 用于获取状态。 4. 在主入口文件 `main.js` 中导入并挂载 Vuex store: ```javascript // main.js import Vue from 'vue' import App from './App' import store from './store' Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ store, ...App }) app.$mount() ``` 在上述代码中,我们将创建的 Vuex store 对象导入并挂载到 Vue 实例上,这样就可以在整个应用中共享该 store 中定义的状态和方法。 5. 现在,你可以在组件中使用 Vuex 了。例如,在一个组件中获取和修改状态: ```vue <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> <button @click="decrement">Decrement</button> </div> </template> <script> export default { computed: { count() { return this.$store.getters.getCount } }, methods: { increment() { this.$store.dispatch('increment') }, decrement() { this.$store.dispatch('decrement') } } } </script> ``` 在上述代码中,我们通过 `$store` 访问 Vuex store 对象,使用 `getters` 获取状态,使用 `dispatch` 提交 actions。 这样,你就可以在 Uniapp 中使用 Vuex 进行全局状态管理了。希望对你有所帮助!如果有任何疑问,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值