2017-02-06 52 views
0

dashboard.service.ts未處理的承諾拒絕:沒有AuthHttp的提供者! ;區:角在angular2,智威湯遜

import { Injectable } from '@angular/core'; 
import { Http, Response, Headers, RequestOptions } from '@angular/http'; 
import { Observable }  from 'rxjs/Observable'; 
import { AuthHttp } from 'angular2-jwt'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 

constructor (private http: Http,private authHttp: AuthHttp) {} 

當我做到這一點給出了一個錯誤Error: Uncaught (in promise): Error: DI

app.module.ts

import { HttpModule } from '@angular/http'; 
import { AuthModule } from 'angular2-jwt'; 
. 
imports: [ 
    HttpModule, 
    AuthModule 
], 

我如何解決這個?

+0

你有進口的HttpModule在ngmodule?您是否將AuthHttp設置爲ngModule中的提供者? :) – Alex

+0

no .............. –

+0

我更新了我的問題並添加了app.module.ts –

回答

2

試試這個

import { AUTH_PROVIDERS } from 'angular2-jwt'; 

@NgModule({ 
    imports: [ 
     BrowserModule, 
     FormsModule, 
     HttpModule, 
    ], 
    providers: [ 
     ...AUTH_PROVIDERS, 
     // other providers 
    ] 
}) 

export class AppModule { } 

選項2

import { AuthModule, AuthConfigConsts} from 'angular2-jwt'; 

const authConfig: IAuthConfig = { 
    headerName: AuthConfigConsts.DEFAULT_HEADER_NAME, // change to your header name 
    headerPrefix: AuthConfigConsts.HEADER_PREFIX_BEARER, // or null or something else 
    tokenName: AuthConfigConsts.DEFAULT_TOKEN_NAME, // change to your token name 
    tokenGetter:() => localStorage.getItem(authConfig.tokenName) as string, 
    noJwtError: false, 
    noClientCheck: false, 
    globalHeaders: [], 
    noTokenScheme: false 
}; 

@NgModule({ 
    imports: [ 
     BrowserModule, 
     FormsModule, 
     HttpModule, 
     AuthModule.forRoot(authConfig) 
    ], 
}) 

export class AppModule { } 
+0

不工作..... –

+0

我更新我的問題並添加app.module.ts –

+1

等一下! –

相關問題