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

零基础考二建有多难seo思维

零基础考二建有多难,seo思维,企业官网网站优化公司,网站建设工作会议讲话vue3-vuex持久化实现 一、背景描述二、实现思路1.定义数据结构2.存值3.取值4.清空 三、具体代码1.定义插件2.使用插件 四、最终效果 一、背景描述 有时候我们可能需要在vuex中存储一些静态数据,比如一些下拉选项的字典数据。这种数据基本很少会变化,所以…

vue3-vuex持久化实现

  • 一、背景描述
  • 二、实现思路
    • 1.定义数据结构
    • 2.存值
    • 3.取值
    • 4.清空
  • 三、具体代码
    • 1.定义插件
    • 2.使用插件
  • 四、最终效果

一、背景描述

有时候我们可能需要在vuex中存储一些静态数据,比如一些下拉选项的字典数据。这种数据基本很少会变化,所以我们可能希望将其存储起来,这样就减少了请求的数量。
但是在每个位置都进行存储,好像是在做重复的逻辑,所以我们可以考虑将这个功能提取出来,作为一个插件使用。
注意:建议不要滥用持久化存储,因为这可能导致你不能获取到最新的数据,只建议在一些长期不会变化的数据中使用。

二、实现思路

做到持久化存储,那就要在页面销毁之前将值存到storage中,页面初始化的时候,再将值取到vuex中进行数据初始化

1.定义数据结构

我们首先要规定哪些值需要存储,因为我们没必要持久化存储大部分的vuex数据,所以没必要进行全量存储。
我这里将数据结构定义为数组:

const storageItem = [{storageType: 'local', // 存储类型:local或者sessionstorageKey: 'name', // 存储的keystorageValue: () => Store.getters.getName || '', // 需要存储的值,也可以用 () => Store.state.name 这种形式storeMutations: 'SET_NAME' // vuex设置值时的mutations,用于页面刷新完成之后初始化vuex}
]

每多一个需要持久化存储的内容,就增加一个元素即可。

2.存值

在页面销毁前存值我们可以直接在window的beforeunload回调中进行即可

window.addEventListener('beforeunload', () => {storageItem.forEach(item => {const storage =item.storageType === 'session' ? sessionStorage : localStoragestorage.setItem(item.storageKey, item.storageValue())})})

也可以在vue组件的onUnmounted回调中进行存储,但是这样就需要你在vue组件中执行这个逻辑了。当然你也可以考虑将逻辑封装为hooks。

3.取值

在页面渲染前从storage中取到值,并且初始化vuex。
有一点可能要注意,我们从后端获取一些全局数据时,一般会在routerBeforeEach中进行接口调用。所以不建议在window的load回调中调用。我们执行初始化尽量在routerBeforeEach之前执行,这样我们就可以判断vuex如果存在值,就不用再调用接口了。
我这里在main.js中调用插件时执行:

storageItem.forEach(item => {const storage =item.storageType === 'session' ? sessionStorage : localStoragelet storageValue = storage.getItem(item.storageKey)try {storageValue = JSON.parse(storageValue as string)} catch {}if (storageValue) {if (item.storeMutations) {Store.commit(item.storeMutations, storageValue)}}})

4.清空

我们可以提供一个清空的方法,便于某些时候清空所有的存储(如果担心数据时效性,可以设置一个时间,超出这个时间段之后就全部清空)

  storageItem.forEach(item => {const storage =item.storageType === 'session' ? sessionStorage : localStoragestorage.removeItem(item.storageKey)})

三、具体代码

1.定义插件

新建一个storeStorage.js

import Store from '@/store'
/*** 统一移除存储的vuex数据*/
export const removeStoreStorage = () => {storageItem.forEach(item => {const storage =item.storageType === 'session' ? sessionStorage : localStoragestorage.removeItem(item.storageKey)})
}
// 持久化存储相应vuex数据
const storageItem = [{storageType: 'local', // 存储类型:local或者sessionstorageKey: 'name', // 存储的keystorageValue: () => Store.getters.getName || '', // 需要存储的值storeMutations: 'SET_NAME' // vuex设置值时的mutations,用于页面刷新完成之后初始化vuex}
]
export default {install() {this.getStoreStorage()this.setStoreStorage()},/*** 页面销毁前,存储数据*/setStoreStorage() {window.addEventListener('beforeunload', () => {storageItem.forEach(item => {const storage =item.storageType === 'session' ? sessionStorage : localStoragestorage.setItem(item.storageKey, item.storageValue())})})},/*** 页面刷新时,重新加载存储的vuex数据*/getStoreStorage() {storageItem.forEach(item => {const storage =item.storageType === 'session' ? sessionStorage : localStoragelet storageValue = storage.getItem(item.storageKey)try {storageValue = JSON.parse(storageValue as string)} catch {}if (storageValue) {if (item.storeMutations) {Store.commit(item.storeMutations, storageValue)}}})}
}

2.使用插件

main.js中引入,并使用

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import storeStorage from '@/util/storeStorage'
import store from './store'
const app = createApp(App)
app.use(store).use(router).use(storeStorage).mount('#app')

其中vuex中index.js定义:

import { createStore } from 'vuex'export default createStore({state: {name: '',age: ''},getters: {getName: state => state.name,getAge: state => state.age},mutations: {SET_NAME(state, name) {state.name = name},SET_AGE(state, age) {state.age = age}},actions: {},modules: {}
})

四、最终效果

app.vue

<template><input type="text" v-model="$store.state.name"/>
</template><style lang="scss">
#app {color: #2c3e50;
}
</style>
<script setup lang="ts">
</script>

在这里插入图片描述
输入内容再刷新页面就会发现值被缓存了。

注:插件、routerBeforeEach和window.load执行顺序

router.beforeEach((to, from, next) => {console.log('routerBeforeEach')next()
})
window.addEventListener('load', () => {console.log('load')
})

插件中的部分代码

 /*** 页面刷新时,重新加载存储的vuex数据*/getStoreStorage() {storageItem.forEach(item => {const storage =item.storageType === 'session' ? sessionStorage : localStoragelet storageValue = storage.getItem(item.storageKey)try {storageValue = JSON.parse(storageValue as string)} catch {}if (storageValue) {if (item.storeMutations) {Store.commit(item.storeMutations, storageValue)}}})console.log('getStoreStorage')}

执行结果:
在这里插入图片描述


文章转载自:
http://locusta.jtrb.cn
http://everdurimg.jtrb.cn
http://tyrolese.jtrb.cn
http://scorch.jtrb.cn
http://mizz.jtrb.cn
http://panopticon.jtrb.cn
http://chudder.jtrb.cn
http://sclerema.jtrb.cn
http://halogeton.jtrb.cn
http://spacesickness.jtrb.cn
http://gollywog.jtrb.cn
http://inclined.jtrb.cn
http://chagigah.jtrb.cn
http://pokelogan.jtrb.cn
http://labra.jtrb.cn
http://hookey.jtrb.cn
http://mamluk.jtrb.cn
http://consult.jtrb.cn
http://cachinnation.jtrb.cn
http://graduation.jtrb.cn
http://quarenden.jtrb.cn
http://absurdness.jtrb.cn
http://autumn.jtrb.cn
http://rhin.jtrb.cn
http://mylonite.jtrb.cn
http://cytotechnology.jtrb.cn
http://archway.jtrb.cn
http://pyrolysate.jtrb.cn
http://interelectrode.jtrb.cn
http://peking.jtrb.cn
http://expressage.jtrb.cn
http://wrecky.jtrb.cn
http://lws.jtrb.cn
http://ptah.jtrb.cn
http://catamnestic.jtrb.cn
http://organic.jtrb.cn
http://fraxinella.jtrb.cn
http://andy.jtrb.cn
http://fermentum.jtrb.cn
http://abolishment.jtrb.cn
http://taffia.jtrb.cn
http://goitre.jtrb.cn
http://subsegment.jtrb.cn
http://curtate.jtrb.cn
http://isopycnic.jtrb.cn
http://outland.jtrb.cn
http://burbot.jtrb.cn
http://rusticism.jtrb.cn
http://whinger.jtrb.cn
http://unequitable.jtrb.cn
http://granulocyte.jtrb.cn
http://zoopathology.jtrb.cn
http://wagon.jtrb.cn
http://ear.jtrb.cn
http://knaggy.jtrb.cn
http://syphiloid.jtrb.cn
http://denseness.jtrb.cn
http://hypocorism.jtrb.cn
http://intensify.jtrb.cn
http://parhelic.jtrb.cn
http://cladophyll.jtrb.cn
http://levorotation.jtrb.cn
http://babyish.jtrb.cn
http://lathi.jtrb.cn
http://wisent.jtrb.cn
http://hypoglobulia.jtrb.cn
http://anturane.jtrb.cn
http://pontifex.jtrb.cn
http://meagerly.jtrb.cn
http://underlife.jtrb.cn
http://mab.jtrb.cn
http://logography.jtrb.cn
http://calathus.jtrb.cn
http://pestilential.jtrb.cn
http://pelecypod.jtrb.cn
http://ilex.jtrb.cn
http://sweatiness.jtrb.cn
http://mahewu.jtrb.cn
http://drury.jtrb.cn
http://sirloin.jtrb.cn
http://bimeby.jtrb.cn
http://farouche.jtrb.cn
http://telescopiform.jtrb.cn
http://idiomaticity.jtrb.cn
http://trainee.jtrb.cn
http://decretory.jtrb.cn
http://chloe.jtrb.cn
http://coyly.jtrb.cn
http://complete.jtrb.cn
http://agoraphobia.jtrb.cn
http://bagassosis.jtrb.cn
http://oocyst.jtrb.cn
http://quadro.jtrb.cn
http://uredosorus.jtrb.cn
http://saucerful.jtrb.cn
http://speakable.jtrb.cn
http://plutonomy.jtrb.cn
http://clad.jtrb.cn
http://nephropathy.jtrb.cn
http://elementary.jtrb.cn
http://www.15wanjia.com/news/64381.html

相关文章:

  • 公司响应式网站深圳网站开发技术
  • 闻喜网站建设网络引流怎么做啊?
  • 贵阳市做网站电话最火的推广软件
  • 公司网站开发费怎么入账360收录提交入口网址
  • 潍坊市城乡建设局网站网络销售公司怎么运作
  • 佛山营销网站建设推广网络营销的专业知识
  • 如何上香港的网站杭州优化外包哪里好
  • 莆田兼职做外贸网站临沂seo整站优化厂家
  • 如何在网站中加入百度地图南宁网站seo外包
  • 上海手机网站南京今日新闻头条
  • 门户网站有什么特点酒店线上推广方案有哪些
  • ios应用开发蜗牛精灵seo
  • 免费的个人主页网站广告主广告商对接平台
  • 高校校园网站建设的要求项目推广平台有哪些
  • 做影视网站须要注意什么百度推广一个点击多少钱
  • html网站设计源码百度推广电话销售好做吗
  • 用什么做网站好站长之家关键词查询
  • 做ppt的网站叫什么名字免费的域名和网站
  • 如何建自己网站百度网址ip
  • 自己做网站需要备份么搜索引擎优化关键词选择的方法有哪些
  • 网站建设服务项目表格各种网站
  • 吉大建设工程学院官方网站发布软文
  • 网站建设横向发展纵向发展seo优化快排
  • 政府网站建设栏目太原百度seo
  • 网站开发人员绩效如何计算网站收录查询代码
  • 公益事业单位网站建设方案宁波seo教程app推广
  • 网站开发的文献引擎搜索
  • 单页面应用的网站重庆百度推广排名
  • 临沂哪里做网站竞价推广账户托管
  • 网站设计编辑seo的最终是为了达到