简体   繁体   English

将Angular 4升级到7时,服务无法正常工作

[英]Service not working when upgrading Angular 4 to 7

I have upgraded my angular app from 4 to latest version 7. After so many errors I finally got it to work but now there is one problem: services are not working, like it is not giving any error in the console but not working as well. 我已经将我的角度应用程序从4升级到最新版本7.经过这么多错误我终于得到了它的工作,但现在有一个问题:服务不起作用,就像它没有在控制台中给出任何错误但是没有正常工作。

I think the problem is with my Http interceptor and the factory in which I am missing something. 我认为问题出在我的Http拦截器和我失踪的工厂。

Can someone tell me what the issue is, exactly? 有人可以告诉我这是什么问题吗?

Http interceptor 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 factory Http工厂

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

provider in app module app模块中的提供者

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

Whenever I login it just starts loading, nothing else, no error or anything and when I comment out the provider in the app module it says "404 not found error". 每当我登录它只是开始加载,没有别的,没有错误或任何东西,当我在应用程序模块中注释掉提供程序时,它说“404 not found error”。

Any help? 有帮助吗?

Thanks 谢谢

Several changes happened between those versions, won't require much work, I've been there too. 这些版本之间发生了一些变化,不需要太多工作,我也去过那里。

Here is a helper, select origin version and destination, you will get a step by step guide of required changes. 这是一个帮手,选择原始版本和目的地,您将获得所需更改的分步指南。

update.angular.io update.angular.io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM