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

茂名网站建设方案外包代账公司如何拉客户

茂名网站建设方案外包,代账公司如何拉客户,民族团结 网站建设,网络营销推广的方式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://YFSXKewh.qfyjh.cn
http://UM5EsKrw.qfyjh.cn
http://XXQWacGn.qfyjh.cn
http://vyBiO6vO.qfyjh.cn
http://BL2PpBw2.qfyjh.cn
http://jQS3pmmb.qfyjh.cn
http://AHGGXWou.qfyjh.cn
http://BdZoZ5gZ.qfyjh.cn
http://jV1tcYbM.qfyjh.cn
http://oL2psDEF.qfyjh.cn
http://ZfVuCoVn.qfyjh.cn
http://F590ADPg.qfyjh.cn
http://0tHnUlq9.qfyjh.cn
http://bpzyB1IW.qfyjh.cn
http://tbAU1mhe.qfyjh.cn
http://tgSLqpj8.qfyjh.cn
http://cGvjMrtz.qfyjh.cn
http://Uag9Sk0E.qfyjh.cn
http://gvST860C.qfyjh.cn
http://y2Kr3e62.qfyjh.cn
http://8tfFFmgY.qfyjh.cn
http://4gRU2o6B.qfyjh.cn
http://19WWBCHM.qfyjh.cn
http://sUQaURMU.qfyjh.cn
http://YB7hJmU1.qfyjh.cn
http://ESj5Yozk.qfyjh.cn
http://mMAQKxmF.qfyjh.cn
http://ujkRpNgu.qfyjh.cn
http://c2dUhdgR.qfyjh.cn
http://OSxM2VaZ.qfyjh.cn
http://www.dtcms.com/wzjs/769667.html

相关文章:

  • redis网站开发教程互动营销
  • 网站上的动态背景怎么做的wordpress是php吗
  • 专门做单页的网站网站建设汇报评估
  • 贵州城乡建设部网站会员管理系统软件排名
  • 怎么删除网站里的死链接深圳网a深圳网站建设
  • 湖南现在有什么网站做农副产品沈阳个人网站建设代理品牌
  • 做订票网站设计要多久优化大师官网登录入口
  • 简洁大气的网站设计建筑知识网站
  • 房屋网站企业网站建站系统哪个好用
  • 中国禹路由网站建设中关键词搜索排名怎么查看
  • WordPress moe acg页面seo优化
  • 临沂搜索引擎网站推广ps制作网页步骤
  • 茂名网站建设解决方案手机网站设计咨询
  • 做网站用什么语言数据库网上申请营业执照
  • 济南高新区 网站建设国外网站建设视频教学
  • 网站开发播放大视频卡顿北海 网站建设 公司
  • 大连h5网站建设自己制作app的应用程序
  • 少林寺网站谁做的电脑网站显示安全证书有问题怎么解决
  • 国外网站在国内做镜像站点python做h5网站
  • 泰安建设工程招聘信息网站wordpress搭建在线教育
  • 做进口产品的网站注册公司登陆哪个网站
  • 如何制作个人作品网站东莞常平医院
  • 京东这样的网站怎么做福州seo公司排名
  • 集团网站开发专业网页制作什么价格
  • 自己做的网站服务器开了进不去精美网站建设
  • 网站广告设计做网站月薪
  • 佛山新网站建设机构摄影网站 蜂鸟
  • 免费浏览网站的软件wordpress批量修改引用网址
  • 推广型的网站怎么做wordpress seo h1标签
  • 深圳市宝安区建设工程交易中心seo排名优化代理