Angular2 extend service with argument in constructor

I have some problems understanding how to properly distribute the service in angular2, maybe because I don’t understand how to extend the class in typescript.

Superclass

@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;
    }
}

Subclass

@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){}
    )
}

What I want to do is extend some classes with CallService so that the whole class making the calls has an errMsg string property for setting or receiving, but I get this exception:

There is no provider for String!

What did I miss?

+4
source share
2 answers

You can either

  • delete option error

  • make parameter erroroptional

export class CallService{
    constructor(@Optional() private errror:string){

...

export class DownloadService extends CallService{
    constructor(@Optional() private error:string,private http:Http){
  • or tell DI that he must pass
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

+4

constructor(private error:string,private http:Http){ , Http string, string, .

+2

Source: https://habr.com/ru/post/1650679/


All Articles