没有AuthHttp的提供者! Angular2-Jwt提供商问题

时间:2017-04-27 22:30:23

标签: jwt angular2-services angular2-providers angular2-jwt

至少我以为我提供的正确。以下是我的app.module文件的相关片段以及我使用AuthHttp的服务。我按照自述文件中的配置创建了提供AuthHttp的工厂方法,但是在我的服务中无法识别它存在持续存在的问题。我已经阅读了有关嵌套依赖注入的文献,我觉得我正在做正确的事情。

app.module.ts

import { Http, RequestOptions } from '@angular/http';
import { provideAuth, AuthHttp, AuthConfig } from 'angular2-jwt/angular2-jwt';

export function authHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig(), http, options);
}

@NgModule({
    declarations: [
        AppComponent,
        ButtonFormComponent,
...
imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        RouterModule,
        AppRoutingModule
    ],
    providers: [
        {
            provide: LocationStrategy,
            useClass: HashLocationStrategy
        },
        {
            provide: AuthHttp,
            useFactory: authHttpServiceFactory,
            deps: [Http, RequestOptions]
        },

employee.service.ts

import { AuthHttp } from 'angular2-jwt/angular2-jwt';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';

import { ApiSettings } from './api-settings';


@Injectable()
export class EmployeeService {
    api: String;
    auth: String;
    constructor(private http: Http, private authHttp: AuthHttp) {
        this.api = ApiSettings.API;
        this.auth = ApiSettings.Auth;
    }

1 个答案:

答案 0 :(得分:5)

您只需在 app.module.ts 中使用以下导入即可摆脱此问题,此处的重点导入是 AUTH_PROVIDERS 。 另外,请确保在providers数组中包含 AUTH_PROVIDERS

    import { AuthHttp, AUTH_PROVIDERS, provideAuth, AuthConfig } from 
    'angular2-jwt/angular2-jwt';

    @NgModule({
       providers: [AUTH_PROVIDERS]
    })