0

我正在尝试将 angular4 应用程序更新为 angular6,并且我正在使用 angular2-jwt 向 web api 发送身份验证,但是在 angular6 中我发现了这个错误: TypeError: Observable_1.Observable.defer is not a function 请参阅此处的错误

这是我创建令牌的地方

 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 { }
4

1 回答 1

1

我认为您可以使用 Interceptor 来实现您的目的。例如 ;

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

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

{
  provide: HTTP_INTERCEPTORS,
  useClass: AuthInterceptor,
  multi: true
}
于 2018-12-29T13:35:43.730 回答