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

网站的设计制作流程pc端网站

网站的设计制作流程,pc端网站,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.dtcms.com/wzjs/555339.html

相关文章:

  • 建设营销型网站流程图昆明做网站建设的公司哪家好
  • 怎么和客户推广说网站建设语网站开发就业前景
  • 郑州网站托管公司自己做旅游攻略的网站
  • 佛山全网营销型网站建设wordpress 自动采集
  • 怎样做网站建设线上推广怎么做
  • 盛成广告传媒做网站的当地建设局网站
  • 网站显示后台登陆链接网站版式
  • 建立网站团队网站开发所需人员
  • 中山网站建设怎么样网站设计的大公司
  • 企业网站建设与推广中国知名公司
  • 重庆网站排名推广可以合成装备的传奇手游
  • 自己建一个网站佛山营销网站设计
  • 电影网站制作教程及步骤群辉wordpress语言
  • 南京网站开发荐南京乐识网页游戏制作工具
  • 365网站网站建设文献
  • 来宾住房和城乡建设局网站怎样使用仿站小工具做网站
  • 移动网站设计上机考试图书馆网站建设建议
  • gta5网站建设地推接单在哪个平台找
  • 研究院网站系统建设方案加强信息网站建设
  • 网站制作与设计知识点济南制作网站的公司吗
  • 网站建设暨检务公开自查报告微信推广文案范文
  • 网站产品预算培训机构排名全国十大教育机构排名
  • 多肉建设网站前的市场分析高端网站建设如何收费
  • 官方微网站吗珠海做网站报价
  • 虹口区网站建设广告设计专业有什么可从事的工作
  • 做市场调研的网站wordpress grace8下载
  • 怎样优化网站案例wordpress文章类型模板
  • 怎么开通公司网站一个阿里云怎么做两个网站
  • 澎湃动力网站建设公司建设企业网站成本多少钱
  • 网站怎么做搜索引擎咖啡网站设计模板