我在ng1中实现了一个LoginInfoService。
要在混合角度应用程序中使用它,根据文档,我已完成以下操作 -
创建了应用模块
import { NgModule, ElementRef} from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UpgradeModule } from '@angular/upgrade/static';
@NgModule({
imports: [BrowserModule,
HttpModule,
JsonpModule,
UpgradeModule
],
declarations: [... ],
providers: [
LoginInfoService,
{ provide: 'LoginInfoService', useFactory: (i: any) => i.get('LoginInfoService'), deps: ['$injector'] },
],
entryComponents:[... ]
})
export class AppModule {
ngDoBootstrap() { /* this is a placeholder to stop the boostrapper from complaining */
}
}
创建了一个包装器ng2服务,如下所示 -
import {Injectable, Inject} from "@angular/core";
@Injectable()
export class LoginInfoService {
constructor( @Inject("LoginInfoService") public loginInfoService: LoginInfoService) {
return loginInfoService;
}
isDirectvInFocus() {
return this.loginInfoService.isDirectvInFocus();
}
isWirelessInFocus() {
return this.loginInfoService.isWirelessInFocus();
}
getAccountInFocus() {
return this.loginInfoService.getAccountInFocus();
}
getLoginType() {
return this.loginInfoService.getLoginType();
}
}
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.documentElement, ['myApp']);//this is angular1 module
});
收到以下错误 -
zone.js:405 Unhandled Promise rejection: No provider for LoginInfoService! ; Zone: ; Task: Promise.then ; Value: Error: No provider for LoginInfoService!
:-1: 2