在Angular 2+ Service

时间:2018-04-16 07:12:10

标签: angularjs angular angular-upgrade

是否可以在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>(...);
    }
}

Main.ts

platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
  const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
  upgrade.bootstrap(document.documentElement, ['app']);
})
.catch(err => console.log(err));

0 个答案:

没有答案