首页 > 解决方案 > 使用@Inject(forwardRef(()) 将数据从管道发送到组件

问题描述

我正在尝试将过滤后的数组从我的管道发送到我的组件,使用@Inject(forwardRef(())如下:

过滤器.pipe.ts

constructor(@Inject(forwardRef(() => OpenItemsComponent)) private ded_open: OpenItemsComponent) {}
transform(items: any[], field: string, value: string[]): any[] {
    ...
    this.ded_open.filteredData(this.newArray)
    ...
}

应用程序.ts

...
filteredData(val) {
    this.currentFilter = val.slice()
    console.log(this.currentFilter) // in currentFilter I will have the filtered array
...

}

我想要的是注入更多组件,因为我在 4 个组件中使用了这个管道。我尝试了这样的事情:

constructor(
@Inject(forwardRef(() => OpenItemsComponent)) private ded_open: OpenItemsComponent,
@Inject(forwardRef(() => ClosedItemsComponent)) private ded_closed: ClosedItemsComponent,
@Inject(forwardRef(() => TrdIComponent)) private trade_open: TradeInvoicesComponent,
@Inject(forwardRef(() => TrdIClosedComponent)) private trade_closed: TradeInvoicesClosedComponent) {
  }

但我明白了

错误错误:未捕获(承诺):错误:StaticInjectorError(AppModule)[FilterPipe -> ClosedItemsComponent]:StaticInjectorError(Platform:core)[FilterPipe -> ClosedItemsComponent]:NullInjectorError:没有ClosedItemsComponent的提供者!

我不能在管道构造函数中注入更多组件?我该如何解决这个问题?感谢您的时间!

(PS - 我知道使用管道不好,但这只是一个演示,不是最终版本)

标签: angularfilterpipe

解决方案


推荐阅读