Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ngx restangular上注入路由器_Angular_Restangular - Fatal编程技术网

在ngx restangular上注入路由器

在ngx restangular上注入路由器,angular,restangular,Angular,Restangular,我正在使用,我想截取响应,如果我得到403,我想能够调用服务来刷新令牌或将用户重定向到登录页面 自动网络服务构造器: constructor(private router: Router, private restangular: Restangular) {} 所以,我在app.module上看到了这个: // This function must return observable var refreshAccesstoken = function () {

我正在使用,我想截取响应,如果我得到403,我想能够调用服务来刷新令牌或将用户重定向到登录页面

自动网络服务构造器:

constructor(private router: Router, private restangular: Restangular) {}
所以,我在app.module上看到了这个:

    // This function must return observable
      var refreshAccesstoken = function () {

        let auth = new AuthenticationService(undefined, undefined);

        // Here you can make action before repeated request
        // This will throw an error
        // ERROR TypeError: Cannot read property 'one' of undefined
        // because refresh method uses a constructor dependencie (restangular to make the http request)
        return auth.refresh();
      };

      RestangularProvider.addErrorInterceptor((response, subject, responseHandler) => {

        if (response.status === 403) {

          // How to redirect user to login page or how to call the
          // refreshAccesstoken() function that will make the http call to get new token?

          refreshAccesstoken()
            .switchMap(refreshAccesstokenResponse => {
              //If you want to change request or make with it some actions and give the request to the repeatRequest func.
              //Or you can live it empty and request will be the same.

              // update Authorization header
              response.request.headers.set('Authorization', 'Bearer ' + refreshAccesstokenResponse)

              return response.repeatRequest(response.request);
            })
            .subscribe(
              res => responseHandler(res),
              err => subject.error(err)
            );

            return false; // error handled
          }
          return true; // error not handled
        });
这是AuthenticationService刷新功能:

refresh() {
        // First way of creating a this.restangular object.
        // Just saying the base URL.
        let baseAuth = this.restangular.one('api').one('refresh');

        return baseAuth.get();
    }
谢谢大家!