championswimmer / vuex-persist

A Vuex plugin to persist the store. (Fully Typescript enabled)
http://championswimmer.in/vuex-persist
MIT License
1.67k stars 116 forks source link

Async Storage with localForage .restored not available. #174

Open WesleyBuck opened 4 years ago

WesleyBuck commented 4 years ago

Currently, we have an older project that we are migrating from javascript to typescript. Implemented typescript router but still using javascript store. Tried npm install @types/vuex-persist with no such package available.

Ended up adding vuex-persist.d.ts, which solved the problem:

import Vue, { VNode } from 'vue'
 import { Store } from 'vuex'

declare module 'vuex' {
    export interface Store<S> {
         restored: Promise<S>
     }
}

Is there a possibility to include types for future users that may experience the same problem?

BasNijhuis commented 4 years ago

Same issue here, temporary fixed it like so:

// hacky solution since type declartion for restored is missing in vuex-persist
// waits for IndexedDB (localForage) to be loaded into vuex so mutations aren't overridden on start up
await ((store as unknown) as { restored: Promise<unknown> }).restored;
TimonPost commented 4 years ago

Use this workaround which worked for me: https://github.com/championswimmer/vuex-persist/issues/15#issuecomment-348308210

rinux55 commented 4 years ago

running into the same issue. the restored prop is not available... unable to use the suggested solutions above.

rinux55 commented 4 years ago

alright the solution is to provide asyncStorage: true in the constructor options. This was not clear from the docs in the section 'notes on localForage'...

jacobcoro commented 4 years ago

Same issue for me, solved with @BasNijhuis solution plus @Rinux55 solution. PLEASE ADD TO DOCS!

jacobcoro commented 4 years ago

This stopped working for some reason, but I got it back by doing

await (store as any).original.restored; in my router guard

and restoreState: key => localForage.getItem(key), in const vuexLocalForage = new VuexPersistence<RootState>({