is it possible to inject router in a parent class in a way that the child classes dont have to pass the router in the constructor?

Viewed 59

I have a service.ts with dependecies Http, Router and is being injected into a parent.ts.

child.ts needs to extend parent classs but should not be bothered about passing service in the constructor. To achieve that, I tried method (a) to implemented parent class instead of trusted method (b) and adding service to providers in app.module.ts with @NgModule decorator.

Issue: I am unable to create injector using method (a)

service.ts

@Injectable()
export class Service {
  constructor(private http: Http,
    private router:Router
    ) {}
}

parent.ts

method(a)

export class Parent {
serv:Service
constructor(private name: string) {
const injector = Injector.create({
       providers:
           [{provide: Service, deps: [Http,Router]}, {provide:Http, deps:[]}, {provide: 
Router, deps:[]}]
         })
this.serv = injector.get(Service)
}
}

method (b)

import { Router } from '@angular/router';
    export class BasetaskComponent {
    constructor(private name: string, private router : Router) {}
}

child.ts

export class Child extends Parent {
constructor() {
super('name')
}
0 Answers
Related