Dbl*_*247 2 inheritance dependency-injection typescript angular
我正在使用Angular 2和TypeScript构建前端应用程序.我有一个Generic Repository类:
export abstract class Repository<T extends IEntity>
{
constructor(protected _http: Http) {}
add(entity: T) {}
delete(entity: T) {}
list() {}
get(id: number) {}
update(entity: T) {}
}
Run Code Online (Sandbox Code Playgroud)
我的服务类然后扩展我的通用存储库类:
export class Service extends Repository<Entity>
{
constructor(protected _http: Http) {
super(Http);
}
}
Run Code Online (Sandbox Code Playgroud)
当我在Parent类中使用依赖注入时出现问题.然后我如何将其转换为子类?
当TypeScript编译时,我收到以下错误:
错误TS2345:'typeof Http'类型的参数不能分配给'Http'类型的参数.Proofy'_ backend'在'typeof Http'类型中缺失
我不知道如何纠正这个错误.任何帮助将不胜感激.
您只需转发您传递的实例(而不是您所做的类型):
export class Service extends Repository<Entity>
{
constructor(protected _http: Http) {
super(_http);
}
}
Run Code Online (Sandbox Code Playgroud)
如果子类中没有引入其他参数,并且也不需要在子类构造函数中执行其他代码,则可以省略构造函数.
| 归档时间: |
|
| 查看次数: |
2586 次 |
| 最近记录: |