我有一个http服务调用,在调度时需要两个参数:
@Injectable()
export class InvoiceService {
. . .
getInvoice(invoiceNumber: string, zipCode: string): Observable<Invoice> {
. . .
}
}
我如何随后将这两个参数传递给我的效果中的this.invoiceService.getInvoice()
?
@Injectable()
export class InvoiceEffects {
@Effect()
getInvoice = this.actions
.ofType(InvoiceActions.GET_INVOICE)
.switchMap(() => this.invoiceService.getInvoice()) // need params here
.map(invoice => {
return this.invoiceActions.getInvoiceResult(invoice);
})
}
答案 0 :(得分:14)
您可以在操作中访问有效内容:
@Injectable()
export class InvoiceEffects {
@Effect()
getInvoice = this.actions
.ofType(InvoiceActions.GET_INVOICE)
.switchMap((action) => this.invoiceService.getInvoice(
action.payload.invoiceNumber,
action.payload.zipCode
))
.map(invoice => this.invoiceActions.getInvoiceResult(invoice))
}
或者您可以使用toPayload
中的ngrx/effects
函数来映射操作的有效负载:
import { Actions, Effect, toPayload } from "@ngrx/effects";
@Injectable()
export class InvoiceEffects {
@Effect()
getInvoice = this.actions
.ofType(InvoiceActions.GET_INVOICE)
.map(toPayload)
.switchMap((payload) => this.invoiceService.getInvoice(
payload.invoiceNumber,
payload.zipCode
))
.map(invoice => this.invoiceActions.getInvoiceResult(invoice))
}
答案 1 :(得分:4)
在@ngrx / effects v5.0中,实用程序函数toPayload
已被删除,自@ ngrx / effects v4.0以来已弃用。
有关详细信息,请参阅:https://github.com/ngrx/platform/commit/b390ef5
现在(自v5.0起):
actions$.
.ofType('SOME_ACTION')
.map((action: SomeActionWithPayload) => action.payload)
实施例:
@Effect({dispatch: false})
printPayloadEffect$ = this.action$
.ofType(fromActions.DEMO_ACTION)
.map((action: fromActions.DemoAction) => action.payload)
.pipe(
tap((payload) => console.log(payload))
);
在:
import { toPayload } from '@ngrx/effects';
actions$.
ofType('SOME_ACTION').
map(toPayload);