当前位置: 首页 > news >正文

大连市社会信用体系建设网站/郑州新闻发布

大连市社会信用体系建设网站,郑州新闻发布,无锡做网站中企动力,天津建设信息网站vuex持久化存储,手动保存到localStorage,退出登录时清空vuex及localStorage 一、vue21. 手动存储到localStoragestore/index.js 2. 使用持久化存储插件store/index.jsstore/modules/otherData.js保存到localStorage 二、vue3(退出登录时清空v…

vuex持久化存储,手动保存到localStorage,退出登录时清空vuex及localStorage

  • 一、vue2
    • 1. 手动存储到localStorage
      • store/index.js
    • 2. 使用持久化存储插件
      • store/index.js
      • store/modules/otherData.js
      • 保存到localStorage
  • 二、vue3(退出登录时清空vuex及localStorage)
    • 1. index.ts
    • 2. store/modules/globalData.ts
    • 3. 在组件中使用App.vue


一、vue2

1. 手动存储到localStorage

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'Vue.use(Vuex)// 本地存储-修改
const storageSet = (key, value) => {localStorage.setItem(key, JSON.stringify(value))
}// 本地存储-获取
const storageGet = (key) => {return JSON.parse(localStorage.getItem(key))
}export default new Vuex.Store({// 数据源 使用:this.$store.state.xxxstate: {user: {} // 用户信息},// 派生数据 使用:this.$store.getters.xxxgetters: {// 获取当前-用户对象GET_USER(state) {state.user = storageGet('STORE_USER') || {}return state.user}},// 更改数据-同步 使用:this.$store.commit('xxx', data)mutations: {// 保存当前-用户对象SET_USER(state, data) {state.user = datastorageSet('STORE_USER', data)}},// mutations装饰器-异步actions: { }
})

2. 使用持久化存储插件

store/index.js

提示:vuex持久化存储

import Vue from 'vue'
import Vuex from 'vuex'// Vuex持久化存储
import VuexPersistence from 'vuex-persist'
const vuexLocal = new VuexPersistence({storage: window.localStorage
})import otherData from "./modules/otherData.js"Vue.use(Vuex)const state = {}const getters = {}const mutations = {}const actions = {}export default new Vuex.Store({state,getters,mutations,actions,modules: {// 模块命名,要在 js文件 声明namespaced: true才有用otherData,},plugins: [vuexLocal.plugin]
})

store/modules/otherData.js

// state
let state = {// 字典数据dictionaryData: [],
}// getters
let getters = {// 字典数据get_dictionaryData(state) {return state.dictionaryData},
}// mutations,以isShow属性为例
let mutations = {// 字典数据change_dictionaryData(state, data) {state.dictionaryData = data},
}// ctions
let actions = {// 这里有两种写法,本质上一样// 写法1,无参asChangeShow(context) {context.commit('changeShow')},// 写法2,无参// asChangeShow({ commit }) {//     commit('changeShow')// }//有参asChangeName({ commit }, data) {commit('changeName', data)}
}
export default {namespaced: true, // 是否开启模块state,getters,mutations,actions
}

保存到localStorage

App.vue

created() {// 在页面加载时读取localStorage里的状态信息if (localStorage.getItem("store")) {this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(localStorage.getItem("store"))))}// 在页面刷新时将vuex里的信息保存到localStorage里window.addEventListener("beforeunload", () => {localStorage.setItem("store", JSON.stringify(this.$store.state))})
}

二、vue3(退出登录时清空vuex及localStorage)

1. index.ts

// store/index.js  
import { createStore } from 'vuex'; // 从 localStorage 中获取初始状态 
const savedState = localStorage.getItem('vuexState');  
const initialState = savedState ? JSON.parse(savedState)  : {}; import globalData from "./modules/globalData"const store = createStore({ state() { return initialState; }, mutations: { clearState(state:any) {// 清空state中的所有数据Objeck.keys(state).forEach(key => {Objeck.keys(state[key]).forEach(childKey => {state[key][childKey] = null}}  // 清空localStoragelocalStorage.clear()}}, actions: { }, getters: { },modules: {globalData,}
}); // 监听状态变化,将状态保存到 localStorage 
store.subscribe((mutation,  state) => { localStorage.setItem('vuexState',  JSON.stringify(state));  
}); export default store; 

2. store/modules/globalData.ts

const state:any = {menuList: []
}
const mutations:any = {change_menuList(state: any, data: any){state.menuList = data}menuList: []
}
export default {namespace: "globalData",state,mutations,
}

使用

<script setup lang="ts"> 
import { useStore } from 'vuex'; const store = useStore(); store.state.globalData.menuList // 获取
store.commit("change_menuList", menu) //更新logout(){store.commit("clearState", menu) //退出登录时清空数据
}</script>

3. 在组件中使用App.vue

<template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> <button @click="decrement">Decrement</button> </div> 
</template> <script setup> 
import { useStore } from 'vuex'; const store = useStore(); const count = store.getters.getCount;  const increment = () => { store.dispatch('increment');  
}; const decrement = () => { store.dispatch('decrement');  
}; 
</script> 
http://www.whsansanxincailiao.cn/news/32053872.html

相关文章:

  • netbean做网站/seo诊断优化方案
  • 专业的内蒙古网站建设/苏州seo排名公司
  • 新乐网站建设/站长工具综合权重查询
  • 怎么用网站推广/网络推广企划
  • 装饰公司资质等级/广东seo推广方案
  • 网站建设的人性分析/培训网络营销的机构
  • 贵州城乡建设厅官网/seo引流什么意思
  • 做风筝网站/上海哪家seo好
  • 凡科网登录管理系统/sem推广优化
  • 为什么一个网站做中英文双语版/网站测试报告
  • 哈尔滨优质的建站销售价格/ciliba最佳磁力搜索引擎
  • centos yum wordpress/seo优化网站优化
  • 做电子商务网站的总结/网络营销策划ppt
  • 河南科技园网站建设/网上卖产品怎么推广
  • 九江网站网站建设/广州网络推广策划公司
  • 网站建设误区图/网络销售推广是做什么的具体
  • 泉州网站建设怎么收费/广告外链平台
  • 网站留言板的作用/seo外包公司一般费用是多少
  • 找人做网站会不会被偷/武汉关键词排名提升
  • 公司做网站的步骤/搜索引擎营销优化策略有哪些
  • 网站开发思路/优化公司流程制度
  • 自做建材配送网站/站长之家点击进入
  • 惠州做网站优化/关键词林俊杰无损下载
  • 济南做网站的/网站优化基本技巧
  • 手机在线做网站/2024年最新一轮阳性症状
  • 洞泾做网站/重庆百度推广的代理商
  • 丹东企业做网站/网站收录怎么做
  • 色无极网站正在建设中/seo网站关键词优化方式
  • 建设wap手机网站制作/产品宣传推广方案
  • 衢州做网站/推广方案框架