如何在 Angular 2 中访问 ngrx 效果中的参数?
How to access parameters in an ngrx effect in Angular 2?
我有一个 http 服务调用,在发送时需要两个参数:
@Injectable()
export class InvoiceService {
. . .
getInvoice(invoiceNumber: string, zipCode: string): Observable<Invoice> {
. . .
}
}
我如何随后将这两个参数传递给我的 Effect 中的 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);
})
}
您可以在操作中访问负载:
@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))
}
或者您可以使用 ngrx/effects
中的 toPayload
函数来映射操作的负载:
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))
}
在@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);
我有一个 http 服务调用,在发送时需要两个参数:
@Injectable()
export class InvoiceService {
. . .
getInvoice(invoiceNumber: string, zipCode: string): Observable<Invoice> {
. . .
}
}
我如何随后将这两个参数传递给我的 Effect 中的 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);
})
}
您可以在操作中访问负载:
@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))
}
或者您可以使用 ngrx/effects
中的 toPayload
函数来映射操作的负载:
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))
}
在@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);