Angular DI Multi Provider机制保证某种顺序吗?
示例:
const TOKEN: OpaqueToken = new OpaqueToken('token');
@Injectable()
class MyService (@Inject(TOKEN) deps: any[]) {/* What is order of deps? */}
@NgModule ({
providers: [
{provide: TOKEN, multi: true, useValue: 1},
{provide: TOKEN, multi: true, useValue: 2},
{provide: TOKEN, multi: true, useValue: 3},
MyService
})
class MyModule {}
答案 0 :(得分:0)
如果你需要在组件X中注入类A,例如,它依赖于其他类,最好的方法是在类A中注入这些类。在组件X中,你仍然需要声明所有的服务。例如: 组件X:
import { Component } from '@angular/core';
import {
CarService, CarService2, CarService3,
EngineService, EngineService2, TiresService
} from './car.services';
car.services.ts
import { Injectable } from '@angular/core';
/// Model ///
export class Car {
}
export class Engine {
}
export class Tires {
}
//// Engine services ///
@Injectable()
export class EngineService {
}
@Injectable()
export class EngineService2 {
}
//// Tire services ///
@Injectable()
export class TiresService {
}
/// Car Services ///
@Injectable()
export class CarService {
id = 'C1';
constructor(
protected engineService: EngineService,
protected tiresService: TiresService) { }
}
@Injectable()
export class CarService2 extends CarService {
id = 'C2';
constructor(
protected engineService: EngineService,
protected tiresService: TiresService) {
super(engineService, tiresService);
}
}
@Injectable()
export class CarService3 extends CarService2 {
id = 'C3';
constructor(
protected engineService: EngineService,
protected tiresService: TiresService) {
super(engineService, tiresService);
}
}
https://stackblitz.com/angular/vomexgeqgdj?file=src%2Fapp%2Fcar.services.ts