将 angular 从 4 升级到 6 时使用 angular2-jwt 和 AuthHttp 的问题

problem in using angular2-jwt and AuthHttp when upgrading angular from 4 to 6

我正在尝试将 angular4 应用程序更新为 angular6,并且我正在使用 angular2-jwt 向网络发送身份验证 api,但是在 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 { }

我认为你可以使用拦截器来达到你的目的。例如;

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
 .......
 .....
}

然后你应该把它放到你的module.ts中,比如

{
  provide: HTTP_INTERCEPTORS,
  useClass: AuthInterceptor,
  multi: true
}