国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

vue中,在本地緩存中讀寫(xiě)數(shù)據(jù)的方法

2024-05-06 15:29:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1.安裝good-storage插件

cnpm i good-storage --save

2.讀/寫(xiě)的方法

common/js/cache.js:

import storage from 'good-storage'const SEARCH_KEY = '__search__'const SEARCH_MAX_LENGTH = 15// compare:findindex傳入的是function,所以不能直接傳valfunction insertArray(arr, val, compare, maxLen) { const index = arr.findIndex(compare) if (index === 0) { return } if (index > 0) { arr.splice(index, 1) } arr.unshift(val) // 插入到數(shù)組最前 if (maxLen && arr.length > maxLen) { arr.pop() // 刪除末位元素 }}// 存儲(chǔ)搜索歷史export function saveSearch(query) { let searches = storage.get(SEARCH_KEY, []) insertArray(searches, query, (item) => { return item === query }, SEARCH_MAX_LENGTH) storage.set(SEARCH_KEY, searches) return searches}// 加載本地緩存的搜索歷史export function loadSearch() { return storage.get(SEARCH_KEY, [])}

3.數(shù)據(jù)用vuex傳遞

在store/actions.js中寫(xiě)入數(shù)據(jù):

import * as types from './mutation-types'import {saveSearch} from 'common/js/cache'export const saveSearchHistory = function({commit, state}, query) { commit(types.SET_SEARCH_HISTORY, saveSearch(query))}

4.組件中調(diào)用vuex

import {mapActions} from 'vuex'// methods內(nèi): saveSearch() {  this.saveSearchHistory(this.query) }, ...mapActions([  'saveSearchHistory' ])

以上這篇vue中,在本地緩存中讀寫(xiě)數(shù)據(jù)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持錯(cuò)新站長(zhǎng)站。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 沂南县| 长丰县| 鱼台县| 南涧| 文山县| 都匀市| 天台县| 绵竹市| 平舆县| 深泽县| 克拉玛依市| 溆浦县| 沅江市| 乐陵市| 永胜县| 碌曲县| 大安市| 青铜峡市| 杭州市| 舟山市| 绥宁县| 桐庐县| 同江市| 北宁市| 肃宁县| 治多县| 当阳市| 佳木斯市| 清徐县| 龙川县| 上栗县| 拉孜县| 惠安县| 陕西省| 东光县| 绥滨县| 信阳市| 泾川县| 剑川县| 丹巴县| 庆云县|