我正在尝试将一个angular4应用程序更新到angular6,并使用angular2-jwt向web发送身份验证,但在angular6中我生成了这个错误:TypeError: Observable_1.Observable.defer is not a function see the error here
这就是我创建令牌的地方
login(model: any) {
const headers = new Headers({ 'Content-type': 'application/json' });
const options = new RequestOptions({ headers: headers });
return this.http
.post(this.baseUrl, model, options)
.map((response: Response) => {
const user = response.json();
if (user) {
localStorage.setItem('token', user.userModel.tokenString);
this.decodedToken = this.jwtHelper.decodeToken(user.userModel.tokenString);
this.userToken = user.userModel.tokenString;
this.currentUserModel = user.userModel;
this.changeuserPhotoUrl(this.currentUserModel.userImage);
}
}).catch(this.handelError);
}这是我的HttpServiceFactory
import { Http, RequestOptions } from "@angular/http";
import { AuthHttp, AuthConfig } from "angular2-jwt";
import { NgModule } from "@angular/core";
export function authenticationHttpServiceFactory(http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig({
tokenName: 'token',
tokenGetter: (() => localStorage.getItem('token')),
globalHeaders: [{ 'Content-Type':'application/json'}]
}),http,options);
}
@NgModule({
providers: [
{
provide: AuthHttp,
useFactory: authenticationHttpServiceFactory,
deps: [Http, RequestOptions]
}
]
})
export class AuthenticationModule { }发布于 2018-12-29 21:35:44
我认为你可以使用Interceptor来实现你的目的。例如:
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
.......
.....
}然后你应该像这样把它放到你的module.ts中
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
}https://stackoverflow.com/questions/53969662
复制相似问题