简体   繁体   English

@NgModule中的依赖项注入不起作用Angular2 2.1.0

[英]Dependency injection in @NgModule not working Angular2 2.1.0

I am trying to correctly set up @NgModule in the latest Angular2 2.1.0. 我正在尝试在最新的Angular2 2.1.0中正确设置@NgModule。 I have some injectable code that relies on other injectable code, and the dependence is not getting initialized for some reason. 我有一些依赖于其他可注入代码的可注入代码,并且由于某种原因而没有初始化依赖项。

When the app starts I get the following error: core.umd.js:3076 TypeError: Cannot read property 'getTokenPromise' of undefined at Object.tokenGetter (app.module.ts:68) 应用启动时,出现以下错误: core.umd.js:3076 TypeError: Cannot read property 'getTokenPromise' of undefined at Object.tokenGetter (app.module.ts:68)

The undefined object in this case is unifiedLogin in the line return unifiedLogin.getTokenPromise(); 在这种情况下,未定义的对象在return unifiedLogin.getTokenPromise();行中是unifiedLogin return unifiedLogin.getTokenPromise();

I expected that UnifiedLogin login would have been injected into AuthHttp and initialized but it is not. 我希望将UnifiedLogin登录名注入到AuthHttp中并进行初始化,但事实并非如此。

This is a fairly large project so I am only including what I hope are the relevant code fragments. 这是一个相当大的项目,所以我只包括我希望的相关代码片段。

This is the section of my @NgModule statement that is in question from app.modules.ts 这是我的@NgModule语句的一部分,与app.modules.ts有关

import { BrowserModule } from '@angular/platform-browser';
import { HttpModule, Http } from '@angular/http';
import { AppRoutingModule } from './app-routing.module';
import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { UnifiedLogin } from './unified-login';
import { AppComponent }  from './app.component';

...

@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule,
        HttpModule,
        AppRoutingModule
    ],
    providers: [
        UnifiedLogin,
        {
            provide: AuthHttp,
            useFactory: (http: Http, unifiedLogin: UnifiedLogin) => {
                return new AuthHttp(new AuthConfig({
                    tokenGetter: (() => {
                        return unifiedLogin.getTokenPromise();
                    })
                }), http);
            },
            deps: [UnifiedLogin]
        }
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

Here is the UnifiedLogin code that I expected to be initialized and injected into AuthHttp but wasn't 这是我希望初始化并注入到AuthHttp中的UnifiedLogin代码,但不是

import { Injectable } from '@angular/core';

...

@Injectable()
export class UnifiedLogin {

    ... 

    public getTokenPromise(): Promise<string> {
        return this._storage.get('id_token');
    }   
}

You need to list the dependencies in deps that you use in the useFactory function. 您需要列出在useFactory函数中使用的deps中的依赖useFactory Since you only list deps: [UnifiedLogin] , the UnifiedLogin gets passed as the first argument to the useFactory function, and the second argument gets nothing. 由于仅列出deps: [UnifiedLogin] ,因此UnifiedLogin作为第一个参数传递给useFactory函数,而第二个参数则什么也没有。

You should add the Http to deps so the arguments are resolved accordingly 您应该将Http添加到deps以便相应地解析参数

{
    provide: AuthHttp,
    useFactory: (http: Http, unifiedLogin: UnifiedLogin) => {
        return new AuthHttp(new AuthConfig({
            tokenGetter: (() => {
                return unifiedLogin.getTokenPromise();
            })
        }), http);
    },
    deps: [Http, UnifiedLogin]   <====== Http
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM