Angular 角度-在继承中使用变量作为类型
我有一个儿童班:Angular 角度-在继承中使用变量作为类型,angular,typescript,inheritance,Angular,Typescript,Inheritance,我有一个儿童班: export class ChildService extends ParentService { protected model = ChildModel; } 在父级中,我想使用子级模型作为http调用响应和承诺的类型 getData(): Promise<this.model> { // doesn't work return this.http.get(this.url) .toPromise() .then(response =&
export class ChildService extends ParentService {
protected model = ChildModel;
}
在父级中,我想使用子级模型作为http调用响应和承诺的类型
getData(): Promise<this.model> { // doesn't work
return this.http.get(this.url)
.toPromise()
.then(response => response as this.model) // doesn't work
.catch(this.handleError);
}
getData():承诺{//不起作用
返回this.http.get(this.url)
.toPromise()
.then(response=>response as this.model)//不起作用
.接住(这个.把手错误);
}
可能吗?使用类型
导出类ChildService扩展了ParentService{
}
班级家长服务{
getData():承诺{
返回this.http.get(this.url)
.toPromise()
.然后(响应=>响应为TType)
.接住(这个.把手错误);
}
}
看起来棒极了!如果同一类中的另一个函数需要使用另一种类型怎么办?@JeremyBelolo你能给我举个例子吗?泛型可以使用多种类型
。方法也可以有自己的本地类型getData:Promise{…}
export class ChildService extends ParentService<ChildModel> {
}
class ParentService<TType> {
getData(): Promise<TType> {
return this.http.get(this.url)
.toPromise()
.then(response => response as TType)
.catch(this.handleError);
}
}