我在使用 ngrx.store 和 states 时遇到了错误(?)。
在我调度新状态并且 reducer 返回新状态之后什么都没有发生。订阅不会收到任何错误消息。 如果我删除 switchMap,代码就会起作用并且订阅者将获得他应该获得的值。
我尽可能少地减少了我的代码:
public a$:Observable<any>;
constructor(private _userService:UserService, private _store:Store<any>)
{
this.a$=this._store
.select(state=>state)
.switchMap(state=>
{
return Observable.from(["1"]); //for simplicity.
});
this.a$.subscribe(
something=>{
console.log(something);
},
error=>{
console.log(error);
}
);
}
我的 reducer :
export const connectedUserReducer = (state = initialState, action:Action) =>
{
if(action==null || action.type==null)
return state;
switch (action.type) {
case updateConnectedUser:
return Object.assign({},state); <<<-- It comes here and then nothing heppends.<<--
default:
return state;
}
};
我确实将 switchMap 导入为:“import 'rxjs/add/operator/switchMap';”
最佳答案
SwitchMap 是一个 rxjs 运算符,因此如果它是一个错误,那么很可能不是 ngrx/store 错误。
https://plnkr.co/edit/0Ul8S0VlRwuBUAOuYDKG?p=preview
this.userList = this.userListService.userList.switchMapTo(Observable.from([[new User(1, "Mr. Grant")]]));
我正在使用一个旧的plunker,我已经从另一个示例中设置了,但是如果你查看组件,我添加了一个switchMapTo(类似于switchMap,但它只是不将原始Observable的值传递给函数)。一切都切换到新的 Observable 就好了。我不确定代码的其余部分设置,但希望仔细查看这一点可以让您了解代码中可能存在的问题。
根据您发布的代码,老实说,我不明白为什么它不起作用。该错误可能位于您的应用或设置中的其他位置。
关于javascript - Observable.switchMap 不起作用并且没有任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38794280/