【发布时间】:2016-12-15 15:15:17
【问题描述】:
我在理解如何在 angular2 中正确扩展服务时遇到一些问题,可能是因为我不太了解如何在 typescript 中扩展类。
超类
@Injectable()
export class CallService{
constructor(private errror:string){
this.errMsg=errror;
}
private _errMsg:string;
set errMsg(arg0:string){
this._errMsg=arg0;
}
get errMsg():string{
return this._errMsg;
}
}
子类
@Injectable()
export class DownloadService extends CallService{
constructor(private error:string,private http:Http){
super(error)
}
}
App.Component
@Component({
selector:'my-app',
templateUrl:'app/app.component.html',
styleUrls:['app/app.component.css'],
directives:[ROUTER_DIRECTIVES,WaitComponent],
providers:[DownloadService,SwitcherService,CallService,WaitService]
})
export class AppComponent{
title:'App'
constructor(
private areadownloadservice:AreaDownloadService,
private waitService:WaitService,
private switcherservice:SwitcherService,
private callService:CallService){}
)
}
我想做的是用 CallService 扩展一些类,这样所有调用的类都会有一个 errMsg 字符串属性来设置或获取,但我得到了这个异常:
没有字符串的提供者!
我错过了什么?
【问题讨论】:
-
问题不是子类。构造函数中的
string类型注解使得注入器去寻找string提供者,并且没有,并且组件中也没有为error定义提供者。 -
好的,按照我上面写的方式扩展服务的正确方法是什么?
-
你希望 Angular DI 向
error参数传递什么?
标签: angular types angular2-services