是否可以在 Angular 2+ 服务中使用 Angular 1.x 服务?这样做时我收到以下错误:
尝试在设置之前获取 AngularJS 注入器。
但是,如果我在 Angular 2 组件中使用 Angular 1 服务(而不是服务),它会起作用。
App.Module.ts:
function getA1Service(i:any) { return i.get('a1Service') }
@NgModule({
declarations: [...],
imports: [...],
entryComponents: [...],
providers: [...,
A2Service,
{ provide: 'a1Service', useFactory: getA1Service, deps: '$injector']},
],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(private upgrade: UpgradeModule) {
}
ngDoBootstrap() {
}
}
A2Service.ts:
export class A2Service {
constructor(private http:HttpClient, @Inject('a1Service') private a1Service) {}
getData() : Observable<data> {
return this.http.get<data>(...);
}
}
主要.ts:
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.documentElement, ['app']);
})
.catch(err => console.log(err));