从角度4升级到7后,服务无法正常工作

时间:2019-05-20 06:10:02

标签: javascript angular typescript http

我已将我的角度应用程序升级到最新版本7。在经历了许多错误之后,我终于使它工作了,但是现在出现了一个问题:服务无法正常工作,就像它在控制台中没有给出任何错误但没有工作正常。

我认为问题出在我的Http拦截器以及缺少某些东西的工厂。

有人可以确切地说出问题是什么吗?

http拦截器

 constructor(
backend: HttpBackend,
private store: Store<any>,
private spinnerService: SpinnerService
) {
super(backend);
}

request(
url: string | HttpRequest<any>,
options?: any
): Observable<any> {
this.showLoader();
return this.tryCatch(super.request(this.getRequestOptionArgs(options)))
}

get(url: string, options?: any): Observable<any> {
url = this.updateUrl(url);
return this.tryCatch(super.get(url));
}

post(
url: string,
body: string,
options?: any
): Observable<any> {
url = this.updateUrl(url);
return this.tryCatch(
  super.post(url, body)
 );
}

put(
url: string,
body: string,
options?: any
): Observable<any> {
url = this.updateUrl(url);
return this.tryCatch(
  super.put(url, body)
 );
}

delete(url: string, options?: any): Observable<any> {
url = this.updateUrl(url);
return this.tryCatch(super.delete(url));
}

patch(
url: string,
body: any,
options?: any
): Observable<any> {
url = this.updateUrl(url);
return this.tryCatch(
  super.patch(url, body)
);
}

private updateUrl(req: string) {
return environment.origin + req;
}

private getRequestOptionArgs(
options?: any
): any {
if (options.headers == null) {
  options.headers = new HttpHeaders();
}
options.headers.append("Content-Type", "application/json");
options.headers.append(
  "Authorization",
  ` Bearer ${sessionStorage.AccessToken}`
);

return options;
}

private tryCatch(obs: Observable<any>) {
return obs.pipe(catchError((error: HttpResponse<any>, caught) => {
  if (error.status === 401 && sessionStorage.AccessToken) {
    sessionStorage.clear();
    this.store.dispatch({ type: "LOGOUT" });
  }
  this.hideLoader();
  return observableThrowError(error);
}));
}

http工厂

export function httpFactory(
xhrBackend: HttpXhrBackend,
store: Store<any>,
spinnerService: SpinnerService
): HttpClient {
return new InterceptedHttp(xhrBackend, store, spinnerService);
}

应用程序模块中的提供商

 {
  provide: HttpClient,
  useFactory: httpFactory,
  deps: [HttpXhrBackend, Store, SpinnerService]
 },

每当我登录时,它就开始加载,没有其他内容,没有错误或任何东西,当我在应用程序模块中注释掉提供程序时,它说“ 404 not found error”。

有帮助吗?

谢谢

0 个答案:

没有答案