typescript - hur man utökar tjänst med beroenden i angular 2
Jag har en föräldertjänst som har några beroenden som
@Injectable()
export class ParentService{
constructor(private http:Http, private customService:CustomService){}
}
och jag vill förlänga tjänsten
@Injectable()
export class ChildService extends ParentService{
constructor (){
super(??) <= typescript now asking to enter two parameters according to ParentServie's constructor
}
}
Redigera-----------------
@Injectable()
export class ParentService{
constructor(private http:Http, private customService:CustomService){}
get(){this.http.get(...)}
}
@Injectable()
export class ChildService extends ParentService{
constructor (private http:Http, private customService:CustomService){
super(http, customService)
}
}
Kan jag då använda det i komponenter?
export class Cmp {
constructor(private childService:ChildService){
this.childService.get()
}
}
Svar #1
Parametrarna för superklassen behöver upprepas och skickas till super-anropet:
@Injectable()
export class ChildService extends ParentService{
constructor (http:Http, customService:CustomService){
super(http, customService);
}
}
Det finns några "hacks" för att kringgå saker som arv och beroendeinjektion
Svar #2
Gör helt enkelt basservice... icke-@Injectable()! Till exempel, du behöver tillhandahålla en metod för att hämta användarprofil i LoginService. Denna metod kommer att vara olika för olika instanser av LoginService, och basklassen får inte veta något om varifrån denna metod kommer: det kan vara en lambda, det kan vara en exporterad funktion, det kan vara en metod i en annan service. För att uppnå detta kan du deklarera en förälder-service som Förälderservice:
// Don't annotate it with @Injectable() ! It's a simple class.
export abstract class BaseLoginService {
constructor(
// This won't be injected automatically,
// child class must call super() and provide value for this argument.
// Note: it's private, can't be accessed outside of BaseLoginService class
private http: HttpClient,
// Method used to retrieve user profile
private profileGetter: () => Observable<UserDetails>,
private router: Router
) {
this.profileGetter().subscribe(); // Do something with this method
}
Sedan utöka det i underklassen:
// Child class must be annotated with @Injectable(),
// if you don't need to extend it further
@Injectable()
export class LoginService extends BaseLoginService {
constructor(
// Note: no public/private/protected here - "http"
// will be just an argument
http: HttpClient,
private profileService: ProfileService, // This is the private field of LoginService class
router: Router,
// Some service used by LoginService class, parent class BaseLoginService
// doesn't need to know about SomeOtherService
private someOtherService: SomeOtherService
) {
super(
http,
// Need lambda here to capture profileService instance. If
// profileService.getUserDetailsMethod doesn't use instance
// fields or methods, then we can simply write
// "profileService.getUserDetailsMethod" (without quotes, obviously).
() => profileService.getUserDetailsMethod(),
router
);
this.someOtherService.doSomething(); // Custom initialization code
}
Obs: i providers-sektionen av modulen ange LoginService istället för den överordnade BaseLoginService:
providers: [
LoginService,
och använd det i komponentklasser:
export class LoginComponent implements OnInit {
constructor(
private loginService: LoginService
) {
}
Om du behöver använda föräldratjänsten (t.ex. i delade komponenter som bara behöver funktionalitet från föräldratjänstklassen), ge då BaseLoginService på detta sätt:
providers: [
{provide: BaseLoginService, useExisting: LoginService}, // For those components which need functionality from base class only
LoginService, // Still need this one for those components which need functionality from child class
