我在angular2 / 4应用中使用 angular2-jwt 插件进行身份验证。但在设置环境后,我可以使用它一次。重建项目后,出现以下错误。
错误:
ERROR in Error遇到静态解析符号值。调用函数' provideAuth',不支持函数调用。考虑使用对导出函数的引用替换函数或lambda,解析/home/user/projects/project/src/app/app.module.ts中的符号AppModule,解决/ home / user / projects / project /中的符号AppModule SRC /应用/ app.module.ts
我的app.module.ts
@NgModule({
imports: [
HttpModule,
BrowserModule,
BrowserAnimationsModule,
AppRoutingModule,
BsDropdownModule.forRoot(),
TabsModule.forRoot(),
ChartsModule,
LaddaModule,
FormsModule,
ToasterModule
],
declarations: [
AppComponent,
LoginComponent,
FullLayoutComponent,
AsideToggleDirective,
DashboardMenuComponent,
],
providers: [
{
provide: LocationStrategy,
// useFactory: authHttpServiceFactory,
// deps: [Http, RequestOptions],
useClass: HashLocationStrategy,
},
AuthenticationService,
AuthGuard,
Auth,
AuthHttp,
provideAuth({
headerName: 'Authorization',
headerPrefix: 'bearer',
tokenName: 'token',
// tokenGetter: (() => localStorage.getItem('token')),
globalHeaders: [{'Content-Type': 'application/json'}],
noJwtError: true
})
],
bootstrap: [AppComponent]
})
收到此错误后,我导出这样的函数:
export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig(), http, options);
}
提供程序中的和导入,但错误将是:
错误错误:未捕获(承诺):错误:没有AuthHttp的提供者!
在提供商中添加 AuthHttp 后,错误将是:
错误错误:未捕获(承诺):错误:没有AuthConfig的提供者!
答案 0 :(得分:1)
提供为
providers: [
AuthHttp,
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, RequestOptions]
}
]
export function authHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig({
headerName: 'Authorization',
headerPrefix: 'bearer',
tokenName: 'token',
// tokenGetter: (() => localStorage.getItem('token')),
globalHeaders: [{'Content-Type': 'application/json'}],
noJwtError: true
}), http, options);
}