Angular 在Ngrx中如何传递值-角度
我是NgRx的新手。我想将一个值传递给一个效果。该值是要在服务中搜索的参数 效果TS文件Angular 在Ngrx中如何传递值-角度,angular,angular7,ngrx,ngrx-effects,angular-state-managmement,Angular,Angular7,Ngrx,Ngrx Effects,Angular State Managmement,我是NgRx的新手。我想将一个值传递给一个效果。该值是要在服务中搜索的参数 效果TS文件 导出类用户效果{ searchParam:string; @效果() 用户$=此.actions$.pipe( ofType(ActionTypes.SEARCH\u用户), map(action=>action.payload),//错误无法识别有效负载 开关映射((有效负载)=> 这个.githubSearch.getUsers(有效负载).pipe( 映射(res=>newuseractions.ge
导出类用户效果{
searchParam:string;
@效果()
用户$=此.actions$.pipe(
ofType(ActionTypes.SEARCH\u用户),
map(action=>action.payload),//错误无法识别有效负载
开关映射((有效负载)=>
这个.githubSearch.getUsers(有效负载).pipe(
映射(res=>newuseractions.getUsersAccess(res)),
catchError(()=>of({type:'[Users-API]Users-Loaded-Error'}))
)
)
);
建造师(
私有操作$:操作,
私有githubSearch:GithubSearchService
) {}
}
如您所见,我有一个名为getParam()
的方法,它订阅了一个包含搜索参数的BehavioralSubject
。我无法在我的效果中调用getParam()
方法。还有别的办法吗?或者直接从不同的.ts
文件将其传递到效果
?我使用有效载荷吗
组件TS文件
onSearch():void{
this.githubSearchService.setSearchParam(this.searchForm.value.searchParam);
this.store.dispatch(new UsersActions.SearchUsers());
这个.searchForm.reset();
}
减速器TS
export class SearchUsers implements Action {
readonly type = ActionTypes.SEARCH_USERS;
constructor(public payload: string) {}
}
case ActionTypes.SEARCH\u用户:
返回动作。有效载荷;
行动TS
export class SearchUsers implements Action {
readonly type = ActionTypes.SEARCH_USERS;
constructor(public payload: string) {}
}
您需要使用操作负载来实现这一点
this.store.dispatch(newusersactions.SearchUsers(searchParam))代码>
然后映射到效果中的动作有效载荷
this.actions$.pipe(
ofType<SearchUsers>(ActionTypes.SEARCH_USERS),
map(action => action.payload),
switchMap((payload) => ...)
此.actions$.pipe(
ofType(ActionTypes.SEARCH\u用户),
映射(action=>action.payload),
开关映射((有效负载)=>…)
您需要使用操作负载来实现这一点
this.store.dispatch(new UsersActions.SearchUsers(searchParam));
然后映射到效果中的动作有效载荷
this.actions$.pipe(
ofType<SearchUsers>(ActionTypes.SEARCH_USERS),
map(action => action.payload),
switchMap((payload) => ...)
此.actions$.pipe(
ofType(ActionTypes.SEARCH\u用户),
映射(action=>action.payload),
开关映射((有效负载)=>…)
您没有在操作上发送任何值。您的操作需要一个字符串类型的值
export class SearchUsers implements Action {
readonly type = ActionTypes.SEARCH_USERS;
constructor(public payload: string) {}
}
这是您的操作,它有一个构造函数,并有一个有效负载发送操作参数上的值,如下所示
this.store.dispatch(new UsersActions.SearchUsers(searchParams));
您没有向action发送任何值,这就是它无法识别action.payload的原因
您没有在操作上发送任何值。您的操作需要一个字符串类型的值
export class SearchUsers implements Action {
readonly type = ActionTypes.SEARCH_USERS;
constructor(public payload: string) {}
}
这是您的操作,它有一个构造函数,并有一个有效负载发送操作参数上的值,如下所示
this.store.dispatch(new UsersActions.SearchUsers(searchParams));
您没有向action发送任何值,这就是它无法识别action的原因。有效载荷您好!感谢您花时间帮助我。我在reducer中遇到了一个错误。我已更新了我的问题。错误无法识别payloadit works tho…它只是无法识别它,所以我将其像下面这样放置映射(action=>action['Payloady'])
并且它可以工作您可能需要添加类型信息:of type(ActionTypes.SEARCH\u USERS)
。这允许TypeScript编译器预期传递给.map()的操作
成为SearchUsers
操作的一个实例。嗨!感谢您花时间帮助我。我在reducer中遇到了一个错误。我已经更新了我的问题。错误不识别有效负载它工作了…它只是不识别它,所以我把它像映射一样放置(操作=>action['payload'))
并且它可以工作您可能需要添加类型信息:of type(ActionTypes.SEARCH\u USERS)
。这允许TypeScript编译器期望传递给.map()
的操作是SearchUsers
操作的实例。