小编Elm*_*aul的帖子

使用 Observables(DRY) 避免重复代码

这是我来自 Angular 的代码(带有重命名的方法):

我有一个函数接口/契约

type IAutocomlete = (
  term: string,
  selectedIds?: string[]
) => Observable<TableFilterQueryResponse[]>;
Run Code Online (Sandbox Code Playgroud)

这是我将来要注入到我的组件中的草稿服务。我会有更多类似实现的方法。

    export class SuperCleanService {
      constructor(
        private dataController: DataController,
        private serviceController: ServiceController
      ) {}
    .....
    private searchAs$: IAutocomlete = (term, selectedIds) => {
        return this.dataController
          .service1GET(term, selectedIds)
          .pipe(
            map((res) =>
              res.map((a) => ({
                viewValue: a.name,
                value: a.id
              }))
            )
          );
      };
      private searchB$: IAutocomlete = (term, selectedIds) => {
        return this.serviceController
          .service1(term, selectedIds)
          .pipe(
            map((res) =>
              res.map((a) => ({
                viewValue: a.id,
                value: a.id
              }))
            ) …
Run Code Online (Sandbox Code Playgroud)

javascript dry rxjs angular

5
推荐指数
1
解决办法
143
查看次数

标签 统计

angular ×1

dry ×1

javascript ×1

rxjs ×1