I have 2 component which same as each other. The only diference is their names and some html headers. Because of that I want to use inheritance.
I have created an abstract service:
export abstract class AbstractFilmListService {
protected readonly API = WebUtils.RESOURCE_HOST_API + 'film/';
constructor(protected client: HttpClient) {
}
deleteFilm(filmId: number) {
return this.client.delete(this.API + filmId);
}
fetchFilms(): Observable<Film[]> {
return this.client.get(this.API) as Observable<Film[]>;
}
}
Sublass 1:
@Injectable({
providedIn: 'root'
})
export class FilmListService extends AbstractFilmListService {
constructor(protected client: HttpClient) {
super(client);
}
}
SubClass 2:
@Injectable({
providedIn: 'root'
})
export class SeriesListService extends AbstractFilmListService{
protected readonly API: string;
constructor(client: HttpClient) {
super(client);
this.API = WebUtils.SERIES_API;
}
}
So far everything is all right. But I want to do same things with components:
FilmListComponent:
@Component({
selector: 'app-film-list',
templateUrl: './film-list.component.html',
styleUrls: ['./film-list.component.css']
})
export class FilmListComponent implements OnInit {
constructor(protected client: HttpClient,
protected router: Router,
protected snackService: SnackService,
protected filmListService: FilmListService // <- Problem is here !
) {
}
//....
}
SeriesListComponent:
@Component({
selector: 'app-series-list',
templateUrl: './series-list.component.html',
styleUrls: ['./series-list.component.css']
})
export class SeriesListComponent extends FilmListComponent implements OnInit {
constructor(protected client: HttpClient,
protected router: Router,
protected snackService: SnackService,
protected filmListService: FilmListService // <- Problem is here!
) {
super(client, router, snackService, filmListService);
}
ngOnInit() {
}
}
I want to FilmListComponent declare AbstractFilmListService instead of FilmListService and when it start to inject service it should injects FilmListService.
Same way SeriesListComponent should declare AbstractFilmListService instead of FilmListService and when it start to inject service it should injects SeriesListService.
I mean something like that:
export class FilmListComponent implements OnInit {
constructor(
// ...
protected filmListService: AbstractFilmListService
) {
this.filmListService = new FilmListService(); // Angular should do something like this...
}
//....
}
Same thing for SeriesListComponent:
export class SeriesListComponent implements OnInit {
constructor(
// ...
protected filmListService: AbstractFilmListService
) {
this.filmListService = new SeriesListService(); // Angular should do something like this...
}
//....
}
I know Angular doesn't create new Services for each Component it uses Singleton Design Pattern for Dependency Injection. In order to make clear myself I have written
"new SeriesListService(), new FilmListService()"
How do I inject different instances for different components even if their types declared same in constuructor?
Update
AppModule:
@NgModule({
declarations: [
AppComponent,
LoginComponent,
HeaderComponent,
HomeComponent,
UserManagementComponent,
ActorComponent,
FilmComponent,
SeriesComponent,
ProductComponent,
SafeUrlPipe,
FilmListComponent,
HeroComponent,
CoverComponent,
SeasonComponent,
ChapterComponent,
SeriesListComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule,
RoutingModule,
TokenModule,
MaterialModule,
NgbTimepickerModule,
HttpInterceptorModule
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {
}
providers: [ { provide: AbstractFilmListService, useClass: SeriesListService } ], but if they are provided in the same module, then the second provided service will override the first. It seems like you want to useAbstractFilmListServiceas an injection token. Could I see the module where you've provided your services?