ディスパッチ時に2つのパラメーターを必要とするhttpサービス呼び出しがあります。
@Injectable()
export class InvoiceService {
. . .
getInvoice(invoiceNumber: string, zipCode: string): Observable<Invoice> {
. . .
}
}
その後、これら2つのパラメーターを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))
}
または、toPayload
関数fromngrx/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))
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加