... non- @Injectable() ! , LoginService. LoginService, , : , , . , :
// Don't annotate it with @Injectable() ! It 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 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
}
:
@Injectable()
export class LoginService extends BaseLoginService {
constructor(
http: HttpClient,
private profileService: ProfileService,
router: Router,
private someOtherService: SomeOtherService
) {
super(
http,
() => profileService.getUserDetailsMethod(),
router
);
this.someOtherService.doSomething();
}
: providers LoginService BaseLoginService:
providers: [
LoginService,
:
export class LoginComponent implements OnInit {
constructor(
private loginService: LoginService
) {
}
(, , ), BaseLoginService :
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