|
2 | 2 |
|
3 | 3 | 本项目是一个[Vuex](https://vuex.vuejs.org/) plugin,用于自动把store中的数据永久存储,例如localStorage、Cookies、localForage。 |
4 | 4 |
|
| 5 | +**注意** |
| 6 | + |
| 7 | +1. 关于`localForage`只是初步这么实现,如果有好的意见可以讨论。 |
| 8 | +2. 本项目支持`TypeScript` |
| 9 | + |
5 | 10 | **package status** |
6 | 11 | [](http://github.com/blackdous/vuex-refesh-storage) |
7 | 12 | [](http://npmjs.com/vuex-refesh-storage) |
@@ -140,3 +145,76 @@ const store = new Store({ |
140 | 145 |
|
141 | 146 | 在`nuxt`中结合`modules`使用。 |
142 | 147 |
|
| 148 | +**store/common/index.js** |
| 149 | + |
| 150 | +```js |
| 151 | + import VuexRefeshStorage from 'vuex-refesh-storage' |
| 152 | + export const vuexCommonState = new VuexRefeshStorage({ |
| 153 | + key: 'common', |
| 154 | + storage: window.sessionStorage |
| 155 | + }) |
| 156 | + export const state = () => ({ |
| 157 | + name: 'common' |
| 158 | + nubOnlinecountDown: 0, |
| 159 | + nubOnlineTime: 60, |
| 160 | + currentNow: new Date().getTime() |
| 161 | + }) |
| 162 | + export const mutations = { |
| 163 | + setNubOnlinecountDown (state, newValue) { |
| 164 | + state.nubOnlinecountDown = newValue |
| 165 | + } |
| 166 | + } |
| 167 | +``` |
| 168 | + |
| 169 | +**store/index.js** |
| 170 | + |
| 171 | +```js |
| 172 | + import { vuexCommonState } from './common' |
| 173 | + export const plugins = [vuexCommonState.install] |
| 174 | +``` |
| 175 | + |
| 176 | +`nuxt`中可以自动读取`store`文件下的`modules`模块并且生成可用的`vuex`代码。 |
| 177 | + |
| 178 | +## 结合localForage使用 |
| 179 | + |
| 180 | +因为`WebSQL`和`IndexedDB`都是异步操作,它们也可以存入对象、typeArray、ArrayBuffer等等类型。以`localForage`为例: |
| 181 | + |
| 182 | +`window.localStorage`为例,传入的`storage`的对象为以下类型(**TypeScript**): |
| 183 | + |
| 184 | +```js |
| 185 | +// 传入的同步storage类型 |
| 186 | + |
| 187 | +interface Storage { |
| 188 | + readonly length: number |
| 189 | + clear(): void |
| 190 | + getItem(key: string): string | null |
| 191 | + key(index: number): string | null |
| 192 | + removeItem(key: string): void |
| 193 | + setItem(key: string, data: string): void |
| 194 | + [key: string]: any |
| 195 | + [index: number]: string |
| 196 | +} |
| 197 | +``` |
| 198 | + |
| 199 | +如果使用类似于`localForage`的类型(**TypeScript**): |
| 200 | + |
| 201 | +```js |
| 202 | + // 传入异步storage类型 |
| 203 | + export interface AsyncStorage { |
| 204 | + _config?: { |
| 205 | + name: string |
| 206 | + } |
| 207 | + getItem<T>(key: string, callback?: (err: any, value: T | null) => void): Promise<T | null>; |
| 208 | + setItem<T>(key: string, value: T, callback?: (err: any, value: T) => void): Promise<T>; |
| 209 | + removeItem(key: string, callback?: (err: any) => void): Promise<void>; |
| 210 | + clear(callback?: (err: any) => void): Promise<void>; |
| 211 | + length(callback?: (err: any, numberOfKeys: number) => void): Promise<number>; |
| 212 | + key(keyIndex: number, callback?: (err: any, key: string) => void): Promise<string>; |
| 213 | + keys(callback?: (err: any, keys: string[]) => void): Promise<string[]>; |
| 214 | + } |
| 215 | +``` |
| 216 | + |
| 217 | +如`localForage`中的`setItem/getItem`都可以是异步的操作,在插件初始化的时候并不能保证`getItem`获取的时机是准确的,所以这里提供了`initAfterFunction`参数,即使`getItem`是异步的也可以保证这个方法会在`getItem`执行完成之后才运行。 |
| 218 | + |
| 219 | +如果在使用`localForage`中的`setItem`、`getItem`方法做一些异步定制操作,可以通过重写`options.setState/options.getState`方法。 |
| 220 | + |
0 commit comments