gpt4 book ai didi

angular - 如何使用 indexedDB 的 promise 在@ngrx/core 中设置initialState

转载 作者:行者123 更新时间:2023-12-04 13:46:15 26 4
gpt4 key购买 nike

我想使用 idb 包在 ngrx 中设置初始状态,该包使用 promise 来获取数据,但每次尝试设置时都会出错。我读到@ngrx 是同步的,这是否意味着它不适用于 promise 。
我尝试过的不同方法:

这是我的 idb 包装器方法,它加载了它工作正常的数据

export function getInitialState(): Promise<any> {
return StorageService.dB.loadInitialState('app-state');
}

以及我尝试设置初始状态的不同方法

方法:1
StoreModule.forRoot(reducers, {initialState: getInitialState})

方法:2
 import { INITIAL_STATE } from '@ngrx/store';
{ provide: INITIAL_STATE, useFactory: getInitialState }

方法:3
export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
return function (state: State, action: any): State {
if(action.type === '@ngrx/store/init') {

}
return reducer(state, action);
};

}

初始化状态被设置为此
ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array(0)}

我收到这个错误
ERROR TypeError: Cannot assign to read only property '__zone_symbol__state' of object '[object Object]'
at resolvePromise (zone.js:810)
at eval (zone.js:876)
at ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (core.js:4747)
at ZoneDelegate.invokeTask (zone.js:424)
at Zone.runTask (zone.js:192)
at drainMicroTaskQueue (zone.js:602)
at ZoneTask.invokeTask [as invoke] (zone.js:503)
at invokeTask (zone.js:1540)
at IDBRequest.globalZoneAwareCallback (zone.js:1566)

*******更新*******

我现在明白了,因为 dee zg 指出每个状态都可以在其效果中具有一个 init 函数,该函数将在加载时触发,我一开始以为这是一个全局一次性事件。这是我用于身份验证状态的一个
 @Effect()
init$: Observable<Action> = defer(() => {
return from(StorageService.readItem('app', Auth.AUTH_KEY)).pipe(
map((data: any) => {
return new Auth.AuthInit(data.state);
})
);
});

最佳答案

有一个init效果中的 Action 在效果运行时触发。在那里你可以调度你的 Action ,获取数据并填充初始状态。
查询 Init Action部分在这里:
https://github.com/ngrx/platform/blob/master/MIGRATION.md

关于angular - 如何使用 indexedDB 的 promise 在@ngrx/core 中设置initialState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47725517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com