我有一个抽象类和它的实现
export abstract class IPrint {
abstract Print(textInput: string): void;
}
export class FilePrint implements IPrint {
Print(textInput: string): void {
console.log("File Print");
}
}
然后介绍Angular DI:
providers:
[
{ provide: IPrint, useClass: FilePrint }
],
我可以像下面这样使用它:
constructor(private _print: IPrint) { }
ngOnInit(): void {
console.log(this._print.Print("HI"))
}
现在我想要 IPrint 的多重实现
export class ScreenPrint implements IPrint {
Print(textInput: string): void {
console.log("Screen Print")
}
}
然后介绍Angular DI:
providers:
[
{ provide: IPrint, useClass: FilePrint },
{ provide: IPrint, useClass: ScreenPrint }
],
当我想使用 IPrint 时,angular 不知道必须使用哪个实现:
constructor(private _print: IPrint) { }