Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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
Angular ng4发生错误后,如何将用户重定向到登录页面?_Angular - Fatal编程技术网

Angular ng4发生错误后,如何将用户重定向到登录页面?

Angular ng4发生错误后,如何将用户重定向到登录页面?,angular,Angular,发生错误后,如何将用户重定向到登录页面 我的代码如下 // Function for settting the default restangular configuration export function RestangularConfigFactory (RestangularProvider, authService) { RestangularProvider.setBaseUrl('http://api.test.com/v1'); // This function mu

发生错误后,如何将用户重定向到登录页面

我的代码如下

// Function for settting the default restangular configuration
export function RestangularConfigFactory (RestangularProvider, authService) {
  RestangularProvider.setBaseUrl('http://api.test.com/v1');

  // This function must return observable
  var refreshAccesstoken = function () {
    // Here you can make action before repeated request
    return authService.functionForTokenUpdate();
  };

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

      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
  });
}

// AppModule is the main entry point into Angular2 bootstraping process
@NgModule({
  bootstrap: [ AppComponent ],
  imports: [ 
    // Importing RestangularModule and making default configs for restanglar
    RestangularModule.forRoot([authService], RestangularConfigFactory),
  ],
})

您可以扩展
Http
来处理错误。在我们的例子中,当我们从服务器发送401时,我们也会在响应体上发送一个重定向地址,如果您选择,您可以执行其他操作

@Injectable()
导出类HttpService扩展Http{
构造函数(后端:ConnectionBackend,defaultOptions:RequestOptions){
超级(后端,默认选项);
}
请求(url:string |请求,选项?:RequestOptionsArgs):可观察{
返回super.request(url,options).catch(this.handleErrors)作为可观察的;
}
handleErrors(响应:响应):可观察的输入{
const body=response.json();
if(response.status==401&&body.redirect){
window.location.href=body.redirect;
}
返回可观察的.throw(body.Message | |'请求失败');
}
}

然后创建一个工厂来导出http服务包装器

export function\u httpServiceFactory(后端:XHRBackend,默认选项:RequestOptions){
返回新的HttpService(后端,defaultOptions);
}

在应用程序根目录下的提供商中,为工厂提供
Http
{
提供:Http,
useFactory:\u httpServiceFactory,
deps:[XHRBackend,RequestOptions]
}

您的意思是我应该使用
window.location.href
设置重定向URL??它能解决任何问题吗?我们选择了window.location.href,因为它是一个非常简单、快速和低级的导航,我们不担心在未经授权的情况下优雅地关闭应用程序。您可以使用任何响应状态或捕获的错误来重定向,我们只是选择在
401
上进行重定向。对于我们来说,
500
错误是在单个api上处理的。“http”的可观察本身将为非成功响应抛出错误。在这一点上,你可以决定你想走多远。这有助于解释更多的
window.location.href
功能。我不得不承认ng1的事情非常简单。