2016-08-16 42 views
3

我在我的Angular2應用程序中使用ngrx/effects,並且自RC5發佈以來,我收到錯誤。這是我的代碼:ngrx/effects switchMap不再在Angular2中工作RC5

import { Injectable } from '@angular/core'; 
import { Effect, Actions } from '@ngrx/effects'; 
import { UserActions } from './user.actions'; 
import { LoginService } from '../login'; 

constructor(
    private updates$: Actions, 
    private userActions: UserActions, 
    private loginService: LoginService 
) {} 

@Effect() loadUser$ = this.updates$ 
    .ofType(UserActions.LOAD) 
    .switchMap(() => this.loginService.loadUserData()) 
    .map(user => this.userActions.loadUserComplete(user.userData)); 

上面的代碼在Angular2 RC4中與ngrx/effects 1.1.1一起工作良好。

但是當我升級到角RC5我得到了錯誤:Unhandled Promise rejection: this.updates$.ofType(...).switchMap is not a function

然後我升級NGRX /效果到最新版本2.0.0-beta.2(其中根據文件應爲用戶定製新RC5 NgModule),但得到了與上述完全相同的錯誤。是的,我在這一步中將whenAction()函數更改爲ofType()

任何想法?

回答

7

您不會自動將所有RxJS運算符都導入到角度2項目中。您是否嘗試手動導入switchMap操作員?

import 'rxjs/add/operator/switchMap'; 

這必須存在於你的代碼的某個地方,也許它是由這之前沒有任何再導入操作第三方庫中導入。

+2

我有這個導入,但我得到一個錯誤,說'屬性'switchMap'不存在類型Observable <{[key:string]:any; }>' – naz786

+0

這對我有用。 – jDub9