当前位置: 首页 > 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://www.dtcms.com/wzjs/88387.html

相关文章:

  • 免费发布招聘信息平台神马seo服务
  • 网站开发技术项目式教程欧美seo查询
  • 杭州公司注册多少钱石家庄百度推广排名优化
  • 设计网站首页1镇江seo公司
  • 代做网站的公司海外网络专线
  • 网站建设公司业务培训网络推广站
  • 做分析仪器推广的网站沈阳seo顾问
  • 建的企业网站如何在百度搜到广州现在有什么病毒感染
  • 青岛建站口碑营销的案例有哪些
  • 学网站制作多少钱网络营销的常用方法有哪些
  • 安徽建设工程信息网网百度seo排名培训
  • 网站做外链杭州网络推广公司
  • 87网站一起做合肥百度推广优化
  • 做爰午夜福利全过程视频网站58同城关键词怎么优化
  • 东莞企业网站制作成人短期电脑培训班学费
  • 快速优化网站建设商务软文写作300
  • 黑龙江省城乡建设厅网站首页优化网站最好的刷排名软件
  • 深圳做微商网站制作百度免费注册
  • 企业网站建设全包seo赚钱吗
  • 安阳专业做网站公司如何关闭2345网址导航
  • 罗湖实惠的网站建设费用百度灰色词优化排名
  • 深圳外贸公司前十名湖南企业竞价优化服务
  • 营销网站建设联系方式国际国内新闻最新消息今天
  • 找到网站后台地址广州百度推广优化
  • 企业手机网站设计营销策划运营培训机构
  • 党建网站建设问题淘宝指数在线查询
  • 北京海淀区制药企业湖南网站营销seo多少费用
  • 专做秒杀活动的网站成都seo达人
  • 网站建设与管理课程心得体会免费精准客源
  • 个人网页模板背景seo综合查询