角继承注入不同的实例

时间:2018-09-05 08:37:25

标签: javascript angular typescript dependency-injection singleton

我有两个彼此相同的成分。唯一的区别是它们的名称和一些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[]>;
  }
}

Sublass 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

以同样的方式SeriesListComponent应该声明AbstractFilmListService而不是FilmListService,并且当它开始注入服务时应该注入SeriesListService

我的意思是这样的:

export class FilmListComponent implements OnInit {

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

}

SeriesListComponent也是如此:

export class SeriesListComponent implements OnInit {

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

}

我知道Angular不会为其使用Singleton Design Pattern进行依赖注入的每个组件创建新服务。为了弄清楚自己,我写了

"new SeriesListService(), new FilmListService()"

即使在构造函数中声明的类型相同,如何为不同的组件注入不同的实例?


更新

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

1 个答案:

答案 0 :(得分:1)

如果我认为它不是单例服务,那么使用providedIn就没有太大意义。

但是要回答您的问题,如果您想要该服务的其他实例,则应在@Component批注中提供该服务:

@Component({
  selector: 'app-series-list',
  templateUrl: './series-list.component.html',
  styleUrls: ['./series-list.component.css'],
  providers: [AbstractFilmListService] // <-- will create a new instance for this component
})