【问题标题】:Angular 8 inject pipe into another pipe does not workAngular 8将管道注入另一个管道不起作用
【发布时间】:2019-10-02 06:45:37
【问题描述】:

从 Angular 7 升级到 Angular 8 后,我的 pipe 停止工作并出现错误:

    TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))

      10 | export class APipe implements PipeTransform {
      11 |
    > 12 |   constructor(
         |               ^
      13 |     private readonly bPipe: BPipe,
      14 |     private readonly cPipe: CPipe) {}

或有类似错误:

Error: Found non-callable @@iterator

我的管道代码:

@Pipe({
  name: 'aPipe'
})
export class APipe implements PipeTransform {

  constructor(
    private readonly bPipe: BPipe,
    private readonly cPipe: CPipe) {}

  transform(someValue: any, args?: any): any {
    if (!someValue) {
      return ' — ';
    }

    if (cond1) {
      return this.bPipe.transform(someValue, ...args);
    }
    else {
      return this.cPipe.transform(someValue, ...args);
    }

    return ' — ';
  }

}

我必须在 Angular8 中将管道显式标记为 @Injectable() 还是有什么问题?

【问题讨论】:

  • 您是否在模块的providers 数组中添加了管道?

标签: angular angular-pipe angular-dependency-injection


【解决方案1】:

编辑

在你的模块中提供它:Source

管道不可注射。他们只是普通的课程。

这意味着如果你想要一个管道到另一个管道,这将通过

来完成
const cPipe = new CPipe(...);

【讨论】:

  • 这是错误的。您可以在组件、服务等中注入管道。这是一个有效的 stackblitz 项目:stackblitz.com/edit/angular-tdvz6q
  • 确实,If you choose to inject your pipe into a class, you must provide it in the providers array of your NgModule.。不知道! (source)
猜你喜欢
  • 2019-02-04
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 2017-03-16
  • 2018-06-28
  • 1970-01-01
  • 2020-05-03
相关资源
最近更新 更多