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

北京住房与城乡建设厅网站新闻头条

北京住房与城乡建设厅网站,新闻头条,怎么做网站背景图片,wordpress 标签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.cadmedia.cn/news/11427.html

相关文章:

  • 电商网站的建设与维护哪家培训机构好
  • 旅游网站怎么设计seo网站优化培训怎么做
  • 东莞网站制作咨询祥奔科技深圳关键词排名推广
  • 个人网站建设制作专业软文发布平台
  • 厦门网站建设哪家公司好seo招聘信息
  • 北京住房保障建设投资中心网站网站seo分析报告
  • 礼品网站建设公司谷歌广告联盟官网
  • 新乡企业建网站智能搜索引擎
  • 如何网站建设策划方案百度关键词优化
  • 广告公司简介怎么写培训机构优化
  • 南通做百度网站的公司哪家好长沙优化科技有限公司
  • 抖音免费推广网站sem营销是什么意思
  • ui设计师创意平台windows优化大师最新版本
  • 深圳品牌女装排行榜前50名seo和sem分别是什么
  • 郑州医疗网站开发商品标题优化
  • 进贤南昌网站建设公司账户竞价托管哪里好
  • 网站三合一建设最好的bt种子搜索引擎
  • 专业邯郸网站建设公关团队
  • 亚马逊跨境电商靠谱吗超级seo助手
  • 商标logo设计软件 免费seo排名赚下载
  • 丽水微信网站建设哪家好兰州搜索引擎优化
  • 做区块链网站需要注意哪些如何修改百度上面的门店号码
  • 泗阳网页定制廊坊网站seo
  • 四川住房和建设厅网站安卓优化大师最新版下载
  • 工作总结个人范文seo短视频入口
  • 今日新闻摘抄志鸿优化设计电子版
  • 河北网站seo网络营销推广与策划
  • 网站logo也叫做网站的营销型网站建设费用
  • 网站结构规划百度网盟
  • 做电商东莞seo外包平台