Angular 如何在具有约束的角度构造函数中注入泛型类型

Angular 如何在具有约束的角度构造函数中注入泛型类型,angular,angular-material,Angular,Angular Material,我有一个数据源实现,用于填充角度材质表。因为我要重用这个类,所以我想使它通用 @Injectable() 导出类TableDataSource实现了DataSource{ 构造函数(专用数据服务:U){ } 连接(collectionViewer:collectionViewer):可观察{ 返回新的可观察(); } 断开连接(collectionViewer:collectionViewer):无效{ // } } 使用此数据源的组件也通过构造函数接收它,注入;像这样: @组件({ 选择器:

我有一个数据源实现,用于填充角度材质表。因为我要重用这个类,所以我想使它通用

@Injectable()
导出类TableDataSource实现了DataSource{
构造函数(专用数据服务:U){
}
连接(collectionViewer:collectionViewer):可观察{
返回新的可观察();
}
断开连接(collectionViewer:collectionViewer):无效{
//
}
}
使用此数据源的组件也通过构造函数接收它,注入;像这样:

@组件({
选择器:“myapp分区”,
templateUrl:'./divisions.component.html',
样式URL:['./divisions.component.scss'],
提供者:[TableDataSource]
})
导出类分区组件实现OnInit{
构造函数(私有数据源:TableDataSource){
}
ngOnInit():void{
}
}
我不知道如何修复下面的错误。类
TableDataSource
已经用
@Injectable()
修饰过了,所以我想我必须以某种方式“配置一个不同的提供程序”?我对这方面的知识太有限,但我确实觉得这是最干净的方式

fail: Microsoft.AspNetCore.SpaServices[0]
          ERROR in src/app/myapp/divisions/table-data-source.service.ts:22:23 - error NG2003: No suitable injection token for parameter 'dataService' of class 'TableDataS
ource'.
    Found U

          22   constructor(private dataService: U) {
                                   ~~~~~~~~~~~
          src/app/myapp/divisions/divisions.component.ts:17:15 - error NG2005: The class 'TableDataSource' cannot be created via dependency injection, as it does not have
 an Angular decorator. This will result in an error at runtime.

    Either add the @Injectable() decorator to 'TableDataSource', or configure a different provider (such as a provider with 'useFactory').


          17   providers: [TableDataSource]
                           ~~~~~~~~~~~~~~~

            src/app/myapp/divisions/table-data-source.service.ts:8:1
                8 @Injectable()
                  ~~~~~~~~~~~~~
                9 export class TableDataSource<T, U extends TableService<T>> implements DataSource<T> {
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
              ...
               81   // }
                  ~~~~~~
               82 }
                  ~
              'TableDataSource' is declared here.
fail:Microsoft.AspNetCore.SpaServices[0]
src/app/myapp/divisions/table data source.service.ts:22:23中出错-错误NG2003:类“TableDataS”的参数“dataService”没有合适的注入令牌
源'。
找到你
22构造函数(专用数据服务:U){
~~~~~~~~~~~
src/app/myapp/divisions/divisions.component.ts:17:15-错误NG2005:无法通过依赖项注入创建类“TableDataSource”,因为它没有
角度装饰器。这将在运行时导致错误。
将@Injectable()装饰器添加到“TableDataSource”,或配置其他提供程序(例如带有“useFactory”的提供程序)。
17提供者:[TableDataSource]
~~~~~~~~~~~~~~~
src/app/myapp/divisions/table数据源.service.ts:8:1
8@Injectable()
~~~~~~~~~~~~~
9导出类TableDataSource实现DataSource{
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
81   // }
~~~~~~
82 }
~
“TableDataSource”在此处声明。

那样不行。Angular无法知道在构造函数中注入什么。不过,给出的错误有点错误。泛型在整个依赖项注入过程中被忽略。但是,您可以执行以下操作:

providers: [
  TableDataSource,
  { provide: TableService, useExisting: DivisionsService }
]
然后像这样将
可注入的
添加到您的
部门服务

@Injectable({
  providedIn: 'root'
})
export class DivisionsService extends TableService<DivisionData> {}

完美!我不介意提供这样的服务。我的问题是:这是实现这一目标的最整洁的方式吗;解耦,可重用代码,etc@321X尽管我从来没有这样做过,但我认为这是一种非常巧妙的方式。是的,我修正了打字错误:)
export abstract class TableService<T> {}