代码之家  ›  专栏  ›  技术社区  ›  Jhoan Nicolas Andres Becerra Q

NGRX-功能未添加到商店的状态

  •  0
  • Jhoan Nicolas Andres Becerra Q  · 技术社区  · 2 年前

    我正在尝试实现授权功能,但无法使效果发挥作用。操作被调度,但效果从未触发,经过大量调试后,我发现状态实际上没有添加到存储中。

    我基本上创建了一个身份验证模块,并在其中向商店注册该功能:

    export const AUTHENTICATION_PROVIDED_CONFIG = new InjectionToken('providedConfig');
    
    @NgModule()
    export class AuthenticationModule {
      constructor(private store: Store) {
        this.store.pipe(select(state => state))
          .subscribe(state => {
            console.log('Current store state - AUTHENTICATION MODULE:', state);
          });
      }
    
      static forRoot(config: AuthenticationConfig) {
        return {
          ngModule: AuthenticationModule,
          imports:[
            EffectsModule.forFeature(authenticationEffects),
            // I also tried doing forFeature(featurekey, reducer).
            StoreModule.forFeature(fromAuthentication.authenticationFeature),
          ],
          providers: [
            AuthenticationFacadeService,
            {
              provide: AUTHENTICATION_PROVIDED_CONFIG,
              useValue: config,
            },
            {
              provide: HTTP_INTERCEPTORS,
              useClass: AuthenticationInterceptor,
              multi: true,
            },
          ]
        }
      }
    }
    

    然后将此authenticationModule导入到我的AppModule中:

    @NgModule({
      declarations: [
        AppComponent,
      ],
      imports: [
        BrowserModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        EffectsModule.forRoot([]),
        StoreModule.forRoot(reducers),
        AuthenticationModule.forRoot(authenticationConfig),
        StoreRouterConnectingModule.forRoot({
          stateKey: 'router'
        }),
        StoreDevtoolsModule.instrument({ maxAge: 25, logOnly: !isDevMode() }),
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule {
      constructor(private store: Store) {
        this.store.pipe(select(state => state))
          .subscribe(state => {
            console.log('Current store state - APP MODULE:', state);
          });
      }
    }
    

    正如您所看到的,我为每个模块的构造函数添加了一些记录器。这是他们记录的内容: enter image description here

    据我所知,授权功能密钥应该出现在这些日志上,对吧?

    我还检查了redux DevTools,我的操作确实被调度了,但效果从未被触发:

    enter image description here

    0 回复  |  直到 2 年前