加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Angular 2中访问ngrx效果中的参数?

发布时间:2020-12-17 08:34:18 所属栏目:安全 来源:网络整理
导读:我有一个http服务调用,在调度时需要两个参数: @Injectable()export class InvoiceService { . . . getInvoice(invoiceNumber: string,zipCode: string): ObservableInvoice { . . . }} 我如何随后将这两个参数传递给我的效果中的this.invoiceService.getInv
我有一个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);
    })
}
您可以访问操作中的有效内容:
@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))
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读