我与 NgRx 合作。这个设计是新的。 我有一个将帖子消息发送到服务器的效果。 帖子消息未发送,在 chrome 网络开发工具 上看不到它。我猜这是订阅 observable 的问题,但我不明白为什么。
效果:
handleInvite$ = createEffect(()=>this.actions$.pipe(
ofActionAndPayload(InviteActions.InviteClicked),
switchMap(({ value, companyId, companyBranches }) => {
const inviteUserRemotely = []
const ownerInvitation = value.branches.find(b => b.role_id === UserRoles.Owner)
const otherInvitations = { email: value.email, branches: value.branches.filter(b
=> b.role_id !== UserRoles.Owner) }
const inviteOwner$ = this.invitationApiService.inviteOwnerFromBranch(companyId, {
email: value.email }).pipe(
catchError(() => EMPTY),
)
const newBranches = (ownerInvitation && companyBranches) ?
companyBranches.map(b => ({ branch_id: b.branch_id, role_id: UserRoles.Owner }))
: value.branches
const newMember: CompanyMemberModel = {
member_id: value.email,
username: value.email,
email: value.email,
first_name: null,
last_name: null,
img: null,
fullName: null,
role_id: -1,
role_name: null,
state_id: UserStatus.Pending,
branches: newBranches,
const inviteOthers$ = this.invitationApiService.inviteFromCompany(companyId, otherInvitations).pipe(
catchError(() => EMPTY),
)
if (ownerInvitation) inviteUserRemotely.push(inviteOwner$)
if (otherInvitations.branches && otherInvitations.branches.length) inviteUserRemotely.push(inviteOthers$)
return forkJoin([inviteUserRemotely])
}),
)));
invitaionService 有一个方法:
inviteOwnerFromBranch(companyId: number, invitation: { email: string }) {
return this.apiService.post(`${apiPrefix}/${companyId}`, { ...invitation,
company_role_id: UserRoles.Owner })
}
apiService 有一个方法 post 可以在其他情况下工作:
post<TResponse, TBody>(
endPoint: string,
body?: TBody,
params?: Dict<string>,
headers?: Dict<string>,
): Observable<TResponse> {
const httpOptions = this.getHttpOptions(params, headers);
return this.track(this.http.post<TResponse>(
this.getUrlToApi(endPoint),
body,
httpOptions,
))
}
在 NgRx 中,效果需要返回新操作或 EMPTY 来表示完成。如果没有返回的操作或 EMPTY,NgRx 可能无法处理流,并且 HTTP 调用将不会执行。
尝试在 api 服务中的 return 语句末尾添加 .subscribe()!