vuex刷新数据丢失问题解决

本文介绍了两种在Vue项目中实现状态持久化的方法:手动利用HTML5本地存储和使用vuex-persistedstate插件。详细步骤包括安装插件、配置存储方式等,适用于Nuxt和标准Vue项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没有特别的幸运,那么就特别的努力!!!

1.手动利用HTML5的本地存储

vuex的state在localStorage或sessionStorage或其它存储方式中取值
缺点:最直观的就是,手动写比较麻烦。

2.利用vuex-persistedstate持久化插件处理

插件的原理其实也是结合了存储方式,只是统一的配置就不需要手动每次都写存储方法

nuxt 项目解决方案:

1.安装vuex-persistedstate插件

yarn add vuex-persistedstate --save
或
npm install vuex-persistedstate  --save

2.新建plugins/vue-persistedstate.js文件

import createPersistedState from 'vuex-persistedstate'

export default ({ store }) => {
  createPersistedState({
    storage: sessionStorage
  })(store)
}

3.配置项nuxt.config.js

plugins: [
	{ src: '@/plugins/vue-persistedstate.js', ssr: false }
  ],

vue项目

1.安装插件

npm install vuex-persistedstate --save
或
yarn add vuex-persistedstate --save

2.引入及配置:在store下的index.js中
使用vuex-persistedstate默认存储到localStorage

import createPersistedState from "vuex-persistedstate"
const store =newVuex.Store({
 state: { ... },
 mutations: { ... },
 actions: { ... },
 plugins: [createPersistedState()]
})

-------------------------------------vuex的使用-----------------------------------------

前提:

1,安装 npm i vuex --save

简略的文件结构 store

src文件
    1. store
        modules文件
            auth.js
            common.js
        index.js
    2.view文件
        home.vue

vuex文件

1.auth.js

const state = {
  areaList:[]
}

const getters = {}

const mutations = {
  infoState (state, payload) {
    state.areaList = payload.areaList
  },
}

const actions = {
  updateArea ({ commit }, payload) {
    commit('infoState', payload)
  },
}

export default {
  state,
  getters,
  actions,
  mutations
}

2.common.js

const state = {
  todos: [
    { id: 1, text: '...', done: true },
    { id: 2, text: '...', done: false }
  ]
}

const getters = {
  areaOptions (state) {
    return state.todos.filter(todo => todo.done)
  },
}

const mutations = {
  // 设置Token

}

const actions = {
  // 更新Token 的方法

}

export default {
  state,
  getters,
  actions,
  mutations
}

3.index.js

/*   Vuex 模块组装 */
import Vue from 'vue'
import Vuex from 'vuex'

import auth from './modules/auth'
import common from './modules/common'

Vue.use(Vuex)

// 导出 store 对象
export default new Vuex.Store({
  // strict: process.env.NODE_ENV !== 'production',
  modules: {
    auth,
    common
  },
  // plugins: process.env.NODE_ENV !== 'production' ? [createLogger()] : []
})

view文件,运用vuex

home.vue

<template>
  <div class="home">
    hello world
  </div>
</template>

<script>
import { mapGetters , mapActions } from 'vuex'

export default {
  name: 'Home',
  components: {},
  data(){
    return{}
  },
  // 监听属性 类似于data概念
  computed: {
    ...mapGetters(['areaOptions'])
  },
  created () {},
  // 生命周期 - 挂载完成(可以访问DOM元素)
  mounted () {
    this.updateArea({ areaList: [{ id: 2, text: '...', done: true }] })
    console.log(this.areaOptions,this.$store.state.auth)
  },
  methods: {
    ...mapActions([
      'updateArea'
    ])
  }
}
</script>

希望能帮助到大家,同时祝愿大家在开发旅途中愉快!!!

拿着 不谢 请叫我“锤” !!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值