uniapp vue2 vuex 持久化

1.vuex的使用

一、uniapp中有自带vuex插件,直接引用即可

二、在项目中新建文件夹store,在main.js中导入

在根目录下新建文件夹store,在此目录下新建index.js文件

index.js

javascript 复制代码
import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
		//公共的变量,这里的变量不能随便修改,只能通过触发mutations的方法才能改变
	},
    mutations: {
		//相当于同步的操作
	},
    actions: {
		//相当于异步的操作,不能直接改变state的值,只能通过触发mutations的方法才能改变
	}
})
export default store

在main.js中导入挂载vuex

javascript 复制代码
import Vue from 'vue'
import App from './App'
import store from './pages/store/index.js'
Vue.prototype.$store = store


Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
	store,
    ...App
})
app.$mount()

2.使用

第一种方式:this. s t o r e 直接操作例如当取值:直接在页面中使用 t h i s . store直接操作 例如当取值:直接在页面中使用this. store直接操作例如当取值:直接在页面中使用this.store.state.变量名

第二种方法:mapState, mapGetters, mapActions, mapMutations

javascript 复制代码
<template>
    <view class="content">
    
    </view>
</template>

<script>
    import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
    //导入
    export default {
        data() {
            return {
            }
        },
        computed: { //computed中注册
            ...mapGetters(['text1']),
            ...mapState([
                'text1'
            ])
        }
        methods: {
            ...mapMutations([]),
            ...mapActions([])
        }
    }
</script>

<style>
</style>

3.vuex持久化

问题:

H5应用中存在Vuex中的数据在刷新页面后丢失了。

原因:

Vuex的 store 中的数据是保存在运行内存中的,当页面刷新时,页面会重新加载 Vue 实例,Vuex数据会重新初始化,导致页面刷新Vuex中的数据丢失的问题。

解决:

使用 vuex-persistedstate 插件

1、在项目目录下执行:npm install --save vuex-persistedstate;

2、修改store/index.js

javascript 复制代码
import Vue from 'vue'
import Vuex from 'vuex'
import persistedState from "vuex-persistedstate"

Vue.use(Vuex)

const store = new Vuex.Store({
	plugins: [
		persistedState({
			storage: {
				getItem: key => uni.getStorageSync(key),
				setItem: (key, value) => uni.setStorageSync(key, value),
				removeItem: key => uni.removeStorageSync(key)
			}
		})
	],
	state:{//存放状态
        "username":'',
        "userid":''
    },
	mutations:{
		//...
	},
	actions:{
		//...
	},
	//...
})

export default store

使用后浏览器打开用控制台调试可看数据存放在 window.localStorage.vuex

这里其实就和调用uniapp提供的数据缓存API存放的数据位置一样了,官方文档链接:https://uniapp.dcloud.io/api/storage/storage.html

注意:在APP-PLUS环境下,这个数据在应用退出时默认不会被清空(或者说初始化)。

原文链接:https://blog.csdn.net/Mr_Bobcp/article/details/125876232

相关推荐
全栈项目管理程序猿1 小时前
ArcGIS JS 基础教程(18):SceneLayer 场景图层
javascript
leoZ2311 小时前
第 2 篇:搭建地基——Vue3 + Vite + Tailwind v4 + shadcn-vue
前端·javascript·vue.js·人工智能·目标检测·数据挖掘·语音识别
七牛开发者2 小时前
拆解 dsh:Session 的事件溯源与状态重建
javascript·github·agent
梨想橙汁2 小时前
JS BOM 浏览器对象:定时器与同步异步底层理解
前端·javascript
吴长建先生2 小时前
支付回调接口设计与代码规范:如何提升团队工程化能力
vue.js
梨想橙汁2 小时前
ES6+ 必用新特性:箭头函数、解构、模板字符串、扩展运算符
前端·javascript
梨想橙汁2 小时前
DOM 与 JS 事件:页面元素操作、事件冒泡、事件委托实战
前端·javascript
一个游离的指针2 小时前
浏览器的渲染原理
前端·javascript
zttbee2 小时前
vue2的API大白话讲解
前端·vue.js·前端框架
ynchyong3 小时前
JavaScript Promise 实战:.then() 与 .catch() 的区别及回调函数封装指南
开发语言·javascript·ecmascript·promise·then