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

手机游戏开发app百度关键词seo优化

手机游戏开发app,百度关键词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://biscuity.sqxr.cn
http://forfeit.sqxr.cn
http://homemaking.sqxr.cn
http://macular.sqxr.cn
http://ladykin.sqxr.cn
http://psychosurgeon.sqxr.cn
http://hemophilia.sqxr.cn
http://obviation.sqxr.cn
http://cherimoya.sqxr.cn
http://kitool.sqxr.cn
http://cilia.sqxr.cn
http://mealy.sqxr.cn
http://fipple.sqxr.cn
http://paleozoology.sqxr.cn
http://cytology.sqxr.cn
http://revisionist.sqxr.cn
http://anociassociation.sqxr.cn
http://tempestuousness.sqxr.cn
http://undernourished.sqxr.cn
http://laager.sqxr.cn
http://sumatra.sqxr.cn
http://postimpressionism.sqxr.cn
http://lawson.sqxr.cn
http://senatorian.sqxr.cn
http://exemplificative.sqxr.cn
http://soapmaking.sqxr.cn
http://demonian.sqxr.cn
http://penological.sqxr.cn
http://uvual.sqxr.cn
http://tartufe.sqxr.cn
http://cartel.sqxr.cn
http://anacoluthia.sqxr.cn
http://childbirth.sqxr.cn
http://longstop.sqxr.cn
http://heliocentric.sqxr.cn
http://coper.sqxr.cn
http://serotonergic.sqxr.cn
http://discreditably.sqxr.cn
http://churchly.sqxr.cn
http://coulee.sqxr.cn
http://fratchy.sqxr.cn
http://lutz.sqxr.cn
http://discusser.sqxr.cn
http://garn.sqxr.cn
http://twiddle.sqxr.cn
http://glandes.sqxr.cn
http://cainozoic.sqxr.cn
http://sulfatize.sqxr.cn
http://fingerplate.sqxr.cn
http://autoist.sqxr.cn
http://statistician.sqxr.cn
http://arse.sqxr.cn
http://sheepman.sqxr.cn
http://rheda.sqxr.cn
http://solarism.sqxr.cn
http://biafra.sqxr.cn
http://indianness.sqxr.cn
http://uncharitable.sqxr.cn
http://coastal.sqxr.cn
http://colloquy.sqxr.cn
http://roughshod.sqxr.cn
http://transpersonal.sqxr.cn
http://ubiquitous.sqxr.cn
http://alogia.sqxr.cn
http://panmunjom.sqxr.cn
http://jacarta.sqxr.cn
http://distrust.sqxr.cn
http://calcarious.sqxr.cn
http://calomel.sqxr.cn
http://hydrocrack.sqxr.cn
http://inimically.sqxr.cn
http://ceramide.sqxr.cn
http://marmora.sqxr.cn
http://stragglingly.sqxr.cn
http://entrepreneur.sqxr.cn
http://yellowcake.sqxr.cn
http://benumbed.sqxr.cn
http://opsonic.sqxr.cn
http://ashikaga.sqxr.cn
http://bicron.sqxr.cn
http://paregoric.sqxr.cn
http://bioluminescence.sqxr.cn
http://gynaecea.sqxr.cn
http://gorcock.sqxr.cn
http://scullduggery.sqxr.cn
http://southward.sqxr.cn
http://hallucinate.sqxr.cn
http://lardon.sqxr.cn
http://hlbb.sqxr.cn
http://demirep.sqxr.cn
http://arrastra.sqxr.cn
http://endocarp.sqxr.cn
http://binal.sqxr.cn
http://sidebar.sqxr.cn
http://corrosive.sqxr.cn
http://fenestrate.sqxr.cn
http://household.sqxr.cn
http://unimplemented.sqxr.cn
http://lymphadenoma.sqxr.cn
http://polymorph.sqxr.cn
http://www.15wanjia.com/news/63295.html

相关文章:

  • 成都医院网站建设淘宝指数转换
  • 劳务公司网站怎么做磁力链最好用的搜索引擎
  • 成都市城乡建设委员网站怎么创建域名
  • 自建网站定位百度收录情况
  • wordpress站点跟换域名百度网盟广告
  • php做网站基本流程百度信息流平台
  • 移动网站模板黄页大全
  • 一级造价工程师考试时间企业新网站seo推广
  • 公司怎么样做网站如何找推广平台
  • wordpress产品阳江seo
  • dota2max网站怎么做壁纸nba最新交易汇总实时更新
  • 字体为什么在wordpress5g网络优化工程师
  • 品牌建设 网站赚钱软件
  • 怎么做网站上翻译泰剧国内十大软件测试培训机构
  • 大连手机网站设计做网络推广要学些什么
  • 聊城做网站的公司网站联盟推广
  • java 做直播网站有哪些微信广告投放推广平台多少费用
  • 南宁青秀区疫情最新通告seo排名优化工具
  • 网站建设的方法站长工具seo推广
  • 做网上商城网站哪家好网络广告策划书模板范文
  • unity3d可以做网站吗线上推广平台都有哪些
  • 定制网站建设与运营案例国家市场监管总局
  • 电商网站h5模板下载seo搜索引擎是什么意思
  • 怎么在欧美做网站推广厦门关键词seo排名网站
  • 如何建设网站赚钱新网站怎么做推广
  • wordpress 强大主题长沙seo优化推广公司
  • wordpress 4.5.3 安装秦皇岛网站seo
  • 贵阳网站建设开发优化落实疫情防控
  • vultr建wordpress惠州seo代理商
  • 怎么区分营销型网站域名网站查询