如何在 Angular 5 组件中注入 AngularJS 服务?
How do I inject an AngularJS service in an Angular 5 component?
我正在使用 ngUpgrade
将一个 AngularJS 项目迁移到 Angular 5,但是我 运行 在尝试注入 [=58] 时遇到了问题=]我的一个新组件中的 JS 服务。
我按照 Angular's upgrade guide 并创建了一个使用 $injector
的 serviceProvider(请参阅下面的代码),但我不断收到此错误:
core.js:1449 ERROR Error: Uncaught (in promise): Error: Trying to get the AngularJS injector before it being set.
我怀疑我需要在某个地方使用 forwardRef
来解决这个问题,但我无法找出如何以及在哪里(以及为什么)。
按照升级指南的例子,我创建了一个serviceProvider如下:
ajs-升级-providers.ts:
// The AngularJS service I want to use
import { AuthenticationService } from '../ajs/services/authentication.service';
export function authenticationServiceFactory($injector) {
return $injector.get('authentication');
}
export const authenticationServiceProvider = {
provide: AuthenticationService,
useFactory: authenticationServiceFactory,
deps: ['$injector']
};
然后我将其提供给应用程序的 NgModule:
app.module.ts:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UpgradeModule,
],
providers: [
authenticationServiceProvider,
],
bootstrap: [
AppComponent,
],
})
export class AppModule {
}
我bootstrap那个模块使用ngUpgrade:
main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { UpgradeModule } from '@angular/upgrade/static';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
// Import our AngularJS module
import '../ajs/app';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.then(platformRef => {
// Use the upgrade module to bootstrap the hybrid
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.documentElement, ['myAngularJSApp']);
});
如果我理解正确,这应该允许我直接将 AngularJS 服务注入我的组件,如下所示:
login.component.ts
import { Component } from '@angular/core';
import { AuthenticationService } from '../ajs/services/authentication.service';
@Component({
selector: 'my-login-page',
templateUrl: './login-page.component.html'
})
export class LoginPageComponent {
constructor(private authenticationService: AuthenticationService) {
console.log('authentication', authenticationService);
}
}
我可以做些什么来进一步简化这个吗?我已尝试尽可能严格地遵循升级指南,但为什么不起作用?
您需要添加
providers: [AuthenticationService]
在组件声明中,像这样:
import { Component } from '@angular/core';
import { AuthenticationService } from '../ajs/services/authentication.service';
@Component({
selector: 'my-login-page',
templateUrl: './login-page.component.html',
providers: [AuthenticationService]
})
export class LoginPageComponent {
constructor(private authenticationService: AuthenticationService) {
console.log('authentication', authenticationService);
}
}
我正在使用 ngUpgrade
将一个 AngularJS 项目迁移到 Angular 5,但是我 运行 在尝试注入 [=58] 时遇到了问题=]我的一个新组件中的 JS 服务。
我按照 Angular's upgrade guide 并创建了一个使用 $injector
的 serviceProvider(请参阅下面的代码),但我不断收到此错误:
core.js:1449 ERROR Error: Uncaught (in promise): Error: Trying to get the AngularJS injector before it being set.
我怀疑我需要在某个地方使用 forwardRef
来解决这个问题,但我无法找出如何以及在哪里(以及为什么)。
按照升级指南的例子,我创建了一个serviceProvider如下:
ajs-升级-providers.ts:
// The AngularJS service I want to use
import { AuthenticationService } from '../ajs/services/authentication.service';
export function authenticationServiceFactory($injector) {
return $injector.get('authentication');
}
export const authenticationServiceProvider = {
provide: AuthenticationService,
useFactory: authenticationServiceFactory,
deps: ['$injector']
};
然后我将其提供给应用程序的 NgModule:
app.module.ts:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UpgradeModule,
],
providers: [
authenticationServiceProvider,
],
bootstrap: [
AppComponent,
],
})
export class AppModule {
}
我bootstrap那个模块使用ngUpgrade:
main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { UpgradeModule } from '@angular/upgrade/static';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
// Import our AngularJS module
import '../ajs/app';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.then(platformRef => {
// Use the upgrade module to bootstrap the hybrid
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.documentElement, ['myAngularJSApp']);
});
如果我理解正确,这应该允许我直接将 AngularJS 服务注入我的组件,如下所示:
login.component.ts
import { Component } from '@angular/core';
import { AuthenticationService } from '../ajs/services/authentication.service';
@Component({
selector: 'my-login-page',
templateUrl: './login-page.component.html'
})
export class LoginPageComponent {
constructor(private authenticationService: AuthenticationService) {
console.log('authentication', authenticationService);
}
}
我可以做些什么来进一步简化这个吗?我已尝试尽可能严格地遵循升级指南,但为什么不起作用?
您需要添加
providers: [AuthenticationService]
在组件声明中,像这样:
import { Component } from '@angular/core';
import { AuthenticationService } from '../ajs/services/authentication.service';
@Component({
selector: 'my-login-page',
templateUrl: './login-page.component.html',
providers: [AuthenticationService]
})
export class LoginPageComponent {
constructor(private authenticationService: AuthenticationService) {
console.log('authentication', authenticationService);
}
}