角度继承注入不同的实例

问题描述 投票:0回答:1

我有2个相同的组件。唯一的区别是他们的名字和一些HTML标题。因为我想使用继承。

我创建了一个抽象服务:

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[]>;
  }
}

子类1:

@Injectable({
  providedIn: 'root'
})
export class FilmListService extends AbstractFilmListService {
  constructor(protected client: HttpClient) {
    super(client);
  }
}

子类2:

@Injectable({
  providedIn: 'root'
})
export class SeriesListService extends AbstractFilmListService{

  protected readonly API: string;

  constructor(client: HttpClient) {
    super(client);
    this.API = WebUtils.SERIES_API;
  }
}

到目前为止一切都很好。但我想用组件做同样的事情:

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

}

我想FilmListComponent声明AbstractFilmListService而不是FilmListService,当它开始注入服务时,它应该注入FilmListService

同样的方式qazxsw poi应该声明qazxsw poi而不是qazxsw poi,当它开始注入服务时它应该注入SeriesListComponent

我的意思是这样的:

AbstractFilmListService

对于SeriesListComponent也是如此:

FilmListService

我知道Angular不为每个使用Singleton Design Pattern for Dependency Injection的组件创建新的服务。为了弄清楚自己,我写了

SeriesListService

如何为不同的组件注入不同的实例,即使它们的类型在constuructor中声明相同?


更新

的AppModule:

export class FilmListComponent implements OnInit {

  constructor(
             // ...
              protected filmListService: AbstractFilmListService
              ) {
          this.filmListService = new FilmListService(); // Angular should do  something like this...
  }
//....

}
javascript angular typescript dependency-injection singleton
1个回答
1
投票

如果它不是单身人士服务,那么在我看来使用export class SeriesListComponent implements OnInit { constructor( // ... protected filmListService: AbstractFilmListService ) { this.filmListService = new SeriesListService(); // Angular should do something like this... } //.... } 并没有多大意义。

但是要回答你的问题,如果你想要这个服务的不同实例,你应该在"new SeriesListService(), new FilmListService()" 注释中提供服务:

@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 {
}
© www.soinside.com 2019 - 2024. All rights reserved.