我有以下使用NgRx Effect
的代码:
import { Injectable } from "@angular/core";
import { Actions, ofType, Effect } from "@ngrx/effects";
import { SearchContactService } from "../../search-contact.service";
import { Action } from "@ngrx/store";
import { Observable, of } from "rxjs";
import { SearchActionTypes, SearchTagResultAction, SearchKeywordResultAction } from "../actions/search.action";
import { mergeMap, map, debounceTime, distinctUntilChanged, filter } from "rxjs/operators";
@Injectable()
export class SearchEffects {
@Effect()
enterSearch$: Observable<Action> = this.actions$.pipe(
ofType(SearchActionTypes.SearchEnterKeyword),
filter(action => { return action && action.payload.keyword && action.payload.keyword.length }),
debounceTime(300),
distinctUntilChanged(),
mergeMap(action => {
let keyword = action.payload.keyword as string;
if (keyword.startsWith("#")) {
return this.searchContactService.searchTag(keyword.substr(1))
.pipe(
map(data => new SearchTagResultAction({
tags: data,
}))
);
} else {
return this.searchContactService.searchContact(keyword)
.pipe(
map(data => new SearchKeywordResultAction({
contacts: data,
}))
);
}
}),
);
constructor(
private searchContactService: SearchContactService,
private actions$: Actions) {
}
}
代码可以在浏览器中编译并正常运行。但是,VS在每个action
参数处显示错误是很烦人的。
有人有这个问题吗?我该怎么解决?
编辑:我发现这是由于ofType
方法而不是pipe
本身引起的。还是不明白为什么。
编辑2:为ofType
添加类型后,另一个分配错误。在浏览器中,它仍然可以编译并运行。我什至尝试了SearchActions
(我的联合操作类型),仍然遇到相同的问题。
编辑:找到了解决方案:在mergeMap上也使用显式类型:
mergeMap<SearchEnterKeywordAction, SearchActions>(action => {
答案 0 :(得分:1)
您必须输入ofType
运算符:
ofType<SearchEnterKeyword>(SearchActionTypes.SearchEnterKeyword),
自NgRx 7起,您还可以输入Actions
注入的@ngrx/effects
constructor(private actions$: Actions<MyActionsUnion>) {}