鉴于我们没有在任何地方订阅它们,我如何记录效果值?

How can I log values of effects given that we don't subsribe to them anywhere?

据我所知,是的,我们不需要订阅像 observables 这样的效果,因为我们不使用结果, 例如:

const subscribe = source.subscribe(val => console.log(val))

但是可以不订阅就登录吗? 这是我的代码,说我要登录,addUserStats$ 效果。

用户个人资料-effects.ts:

@Injectable()
export class UserProfileEffects {

  constructor(
    private actions$: Actions,
    private userProfileActions: UserProfileActions,
    private userProfile: UserProfile,
    private auth: Authorization
  ) { }

  @Effect()
  updateToken$ = this.actions$
    .ofType(UserProfileActions.UPDATE_TOKEN)
    .map(action => action.payload)
    .map((token: string) => this.auth.accessToken = token)
    .switchMap(token => this.userProfile.getStats(true))
    .map(response => this.userProfileActions.updateData(response));

  @Effect()
  addUserStats$ = this.actions$
    .ofType(UserProfileActions.UPDATE)
    .map(action => action.payload)
    .map((data: any) => this.userProfileActions.addStats(data.items));
}

effects.ts:

export default [
  NowStatEffects,
  UserProfileEffects
];

core.module.ts:

import effects from './effects';
@NgModule({
  imports: [
    CoreStoreModule,
    ...effects.map(effect => EffectsModule.run(effect)),
  ],
  declarations: [
  ],
  exports: [
    CoreStoreModule,
  ],
  providers: [
    ...APP_SERVICES
  ]
})
export class CoreModule {
  constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
    throwIfAlreadyLoaded(parentModule, 'CoreModule');
  }
}

您可以通过链接 do() 运算符来利用可观察流。它将:

Perform a side effect for every emission on the source Observable, but return an Observable that is identical to the source.

  @Effect()
  addUserStats$ = this.actions$
    .ofType(UserProfileActions.UPDATE)
    .map(action => action.payload)
    .do((data: any) => console.log(data.items))
    .map(payload => { /* do something else */ )

或更短的版本:

    .do(console.log)