angular2 在构造函数中使用参数扩展服务
angular2 extend service with argument in constructor
我在理解如何在 angular2 中正确扩展服务方面遇到了一些问题,可能是因为我不太了解如何在 typescript 中扩展 class。
超类
@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 扩展一些 classes,这样所有进行调用的 class 都会有一个 errMsg 字符串 属性 来设置或获取,但是我得到这个例外:
No provider for String!
我错过了什么?
在这行代码 constructor(private error:string,private http:Http){
中,您告诉构造函数实际准备 Http
和 string
服务,但您没有这样的 string
任何地方声明的服务。
您可以
删除error
参数
使 error
参数可选
export class CallService{
constructor(@Optional() private errror:string){
...
export class DownloadService extends CallService{
constructor(@Optional() private error:string,private http:Http){
- 或者告诉 DI 它应该传递什么
bootstrap(AppComponent, [HTTP_PROVIDERS, {provide: 'someToken', useValue: 'someStringValue'}])
export class CallService{
constructor(@Inject('someToken') private errror:string){
...
export class DownloadService extends CallService{
constructor(@Inject('someToken') private error:string,private http:Http){
您也可以使用 OpaqueToken 代替字符串键 ('someToken')。
另见 http://blog.thoughtram.io/angular/2016/05/23/opaque-tokens-in-angular-2.html
我在理解如何在 angular2 中正确扩展服务方面遇到了一些问题,可能是因为我不太了解如何在 typescript 中扩展 class。
超类
@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 扩展一些 classes,这样所有进行调用的 class 都会有一个 errMsg 字符串 属性 来设置或获取,但是我得到这个例外:
No provider for String!
我错过了什么?
在这行代码 constructor(private error:string,private http:Http){
中,您告诉构造函数实际准备 Http
和 string
服务,但您没有这样的 string
任何地方声明的服务。
您可以
删除
error
参数使
error
参数可选
export class CallService{
constructor(@Optional() private errror:string){
...
export class DownloadService extends CallService{
constructor(@Optional() private error:string,private http:Http){
- 或者告诉 DI 它应该传递什么
bootstrap(AppComponent, [HTTP_PROVIDERS, {provide: 'someToken', useValue: 'someStringValue'}])
export class CallService{
constructor(@Inject('someToken') private errror:string){
...
export class DownloadService extends CallService{
constructor(@Inject('someToken') private error:string,private http:Http){
您也可以使用 OpaqueToken 代替字符串键 ('someToken')。
另见 http://blog.thoughtram.io/angular/2016/05/23/opaque-tokens-in-angular-2.html