代码之家  ›  专栏  ›  技术社区  ›  danday74

除非安装了Redux DevTools,否则NGRX Angular应用程序不工作。

  •  3
  • danday74  · 技术社区  · 6 年前

    我们的NGRX Angular应用程序工作得很好,直到我们卸载Redux DevTools,这时我们会得到错误:

    您提供的对象无效,需要流。您可以提供一个可观察的、承诺的、数组的或不可观察的。

    app.module.ts中的商店设置如下:

    StoreModule.forRoot(reducers, {metaReducers}),
    !environment.production ? StoreDevtoolsModule.instrument() : [],
    EffectsModule.forRoot([SimsEffects, FiltersEffects, OmnipresentEffects, UserEffects, InitEffects]),
    StoreRouterConnectingModule.forRoot({stateKey: 'router'})
    

    我们的ngrx reducers/index.ts文件如下:

    export interface AppState {
      router: any,
      omnipresent: OmnipresentState,
      user: UserState
    }
    
    export const reducers: ActionReducerMap<AppState> = {
      router: routerReducer,
      omnipresent: omnipresentReducer,
      user: userReducer
    }
    
    export function resetStore(reducer) {
      return (state, action) => {
        return reducer(action.type === InitActionTypes.ResetStore ? undefined : state, action)
      }
    }
    
    // storeFreeze throws an error early if state is mutated, avoiding hard to debug state mutation issues
    export const metaReducers: MetaReducer<AppState>[] = !environment.production ? [resetStore, storeFreeze] : [resetStore] // tslint:disable-line array-type
    

    以前有人经历过这种情况,或者知道解决方法吗?这个问题存在于dev和prod环境中。

    我们正在运行节点V8.11.3

    编辑 :如果我注释掉这一行,错误就会消失,但显然存储无法初始化:

    @Effect()
    init$: Observable<any> = defer(() => {
      return of(new InitAction())
    })
    

    其中initaction只是一个不执行任何附加效果的操作(目的是帮助调试此操作)。

    2 回复  |  直到 5 年前
        1
  •  1
  •   albanx    5 年前

    就我而言,问题是 https://github.com/angular/angular/issues/25837

    不安装ngrx devtools,不知道为什么,某些部分的导航被认为是在ngzone之外(这实际上是由某个google sdk回调调用的)。如果devtools安装正确,则不会发生这种情况。

    问题是控制台中没有出现错误,必须启用详细日志才能看到警告。 通过包装在ngzone中解决。运行调用:

    constructor(private ngZone: NgZone) {}

    this.ngZone.run(() => this.store.dispatch(buildAuthStartAction(user)));

        2
  •  1
  •   danday74    6 年前

    改变这个解决了它:

    import { defer, of } from 'rxjs/index' // as per my IDE suggestions
    

    到:

    import { defer, of } from 'rxjs' // as per ngrx docs
    

    此外,还将对“rxjs/index”的所有引用替换为“rxjs”