angularjs - 如何在 Angular 5 组件中注入(inject) AngularJS 服务?

标签 angularjs angular angular-upgrade

我正在使用 ngUpgrade 将一个 AngularJS 项目迁移到 Angular 5,但是当我尝试在我的一个新组件中注入(inject) AngularJS 服务时遇到了问题。

我关注了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-upgraded-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 {
}

我使用 ngUpgrade 引导该模块:

ma​​in.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 服务注入(inject)到我的组件中,如下所示:

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);
    }
}

关于angularjs - 如何在 Angular 5 组件中注入(inject) AngularJS 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50393336/

相关文章:

javascript - 在 angularJS 中使用 JSON 显示文件系统结构

angularjs - Angular 和 Express 路由如何在 mean.js 应用程序中协同工作?

angular - 测试 angular if else block 和 subscribe(response => {} block in jasmine

Angular 9 到 10 升级 - Typescript 编译警告 : *. ngtypecheck.ts

angular - 将 Angular 6 更新为 7 - 依赖警告和 typescript 错误

javascript - 使用 Protractor 设置 Angular 模型

javascript - 在 Angular js 中使用 json 数据填充 Highchart

angular - 如何从 ngx-timepicker-field 获取用户输入

javascript - 使用 Angular2 TestBed 模拟具有非具体类接口(interface)参数的服务

angular - 错误 TS2339 : Property 'do' does not exist on type 'Observable<