我如何记录效果的值,因为我们不会在任何地方给它们加载?

时间:2017-04-02 07:21:49

标签: angular rxjs rxjs5

我理解是的,我们不需要订阅像observable这样的效果,因为我们没有消耗结果, 比如:

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

但是可以在没有订阅的情况下登录吗? 这是我的代码,说我要记录,addUserStats $ effect。

用户轮廓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');
  }
}

1 个答案:

答案 0 :(得分:2)

您可以通过链接do()运算符来点击可观察的流。它会:

  

对源Observable上的每个发射执行副作用,但返回与源相同的Observable。

  @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)