所以,我最终使用了接口,而不是这个解决方案的抽象类。而且,当我尝试为我的两个服务使用基类时,继承方法失败了,比如this question。
这是我在 JIT 和 AOT 编译中的工作解决方案:
@Component({
selector: 'invoice-list',
templateUrl: './invoice-list.component.html',
providers: [{ provide: RESTToken, useClass: InvoiceRESTService }],
...
组件中实际使用的服务:
@Injectable()
export class InvoiceRESTService {
public invoiceType: InvoiceType;
constructor(
private _visma: VismaRESTService,
private _fortnox: FortnoxRESTService
) {}
public get instance(): RESTServiceInterface {
if (this.invoiceType === InvoiceType.Visma) {
return this._visma;
} else if (this.invoiceType === InvoiceType.Fortnox) {
return this._fortnox;
} else {
console.log('Please set the invoice type.');
}
}
}
界面本身:
export interface RESTServiceInterface {
getExportDataFunctionURL(): string;
// So far, only for Visma
getAuthUrl?(): string;
getButtonTitlle(): string;
synchData(instanceUid: string, code?: string): Observable<Object>;
updateInvoice(payload: any): Observable<Object>;
// Loading messages
getWaitingMessage(): string;
getSuccessMessage(): string;
getErrorMessage(code: VismaErrorCode | FortnoxErrorCode | BKErrorCode): string;
}
实现接口的服务:
@Injectable({
providedIn: 'root'
})
export class FortnoxRESTService implements RESTServiceInterface {
...
...
...
}
@Injectable({
providedIn: 'root'
})
export class VismaRESTService implements RESTServiceInterface {
...
...
...
}