Angular 使用Abstract在Typescript中创建基本组件
我正在尝试使用列表页中使用的一些默认方法创建一个基本组件,因此这是我的ts文件:Angular 使用Abstract在Typescript中创建基本组件,angular,typescript,Angular,Typescript,我正在尝试使用列表页中使用的一些默认方法创建一个基本组件,因此这是我的ts文件: export abstract class BaselistComponent<T> implements OnInit { source: LocalDataSource = new LocalDataSource(); constructor(private service: BasecrudService<T>, private notificat
export abstract class BaselistComponent<T> implements OnInit {
source: LocalDataSource = new LocalDataSource();
constructor(private service: BasecrudService<T>,
private notification: NotificationService) {
}
ngOnInit(): void {
this.service.findRecords().subscribe((data: T[]) => {
this.source.load(data);
});
}
}
以及服务Impl:
@Injectable({
providedIn: 'root',
})
export class UnidadeService extends BasecrudService<Unidade> {
constructor(http: HttpClient, adapter: Adapter<Unidade>) {
super(http, adapter);
}
getPrefixURL(): string {
return 'unidades';
}
}
@可注入({
providedIn:'根',
})
导出类UnidadeService扩展BasecrudService{
构造函数(http:HttpClient,adapter:adapter){
超级(http,适配器);
}
getPrefixURL():字符串{
返回“unidades”;
}
}
编辑1
这是我的控制台.log(This)
,请注意,我的服务
未定义
编辑2
这是我的项目完整源代码:错误的原因是您在Angular DI中使用了接口 unidate.service.ts
export class UnidadeService extends BasecrudService<Unidade> {
constructor(http: HttpClient, adapter: Adapter<Unidade>) {
^^^^^^^
super(http, adapter);
}
}
导出类UnidadeService扩展BasecrudService{
构造函数(http:HttpClient,adapter:adapter){
^^^^^^^
超级(http,适配器);
}
}
这会导致DI错误,因此您无法初始化
UnidadeService
initialized工作正常此处您可以提供有关您的环境的更多信息吗?角度版和其他我用更多的信息编辑了这篇文章,我用的是角度版7@Jota.Toledo我不知道为什么你的代码有效而我的代码无效。你是否在任何模块中提供任何服务(抽象或具体)?
@Injectable({
providedIn: 'root',
})
export class UnidadeService extends BasecrudService<Unidade> {
constructor(http: HttpClient, adapter: Adapter<Unidade>) {
super(http, adapter);
}
getPrefixURL(): string {
return 'unidades';
}
}
export class UnidadeService extends BasecrudService<Unidade> {
constructor(http: HttpClient, adapter: Adapter<Unidade>) {
^^^^^^^
super(http, adapter);
}
}