至少我以为我的提供正确。以下是我的app.module文件的相关片段和我使用authhttp的服务。我遵循了Readme中的配置,以创建出厂方法来提供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]
},
雇员.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;
}
您只需在您的 app.module.ts 中使用以下导入来摆脱此问题,这是您的关键导入IS, auth_providers。另外,请确保在提供商数组中包括 auth_providers 。
import { AuthHttp, AUTH_PROVIDERS, provideAuth, AuthConfig } from
'angular2-jwt/angular2-jwt';
@NgModule({
providers: [AUTH_PROVIDERS]
})