2017-04-02 20 views
0

我明白了什麼是我們不需要訂閱喜歡觀測的影響,因爲我們沒有消費的結果, 例如像:我如何記錄效果的價值,因爲我們不會向任何地方付費?

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'); 
    } 
} 
+3

使用'。做(的console.log)'。 [This](http://blog.angular-university.io/debug-rxjs/)也可能有用。 – Sasxa

+1

偉大的工作,你可以發佈它作爲答案,如果你有'.map(console.log)'的差異的解釋' – ishandutta2007

+1

@Sasxa你可能想提供它作爲答案:-) – echonax

回答

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) 
相關問題