2016-07-27 102 views
1

如果是401響應,如何攔截服務器端點響應並將aurelia應用程序重定向到登錄頁面?如果服務器返回401,則重定向到登錄頁面回覆

我試過 「withInterceptor(responseError(){...})」 奧裏利亞取客戶端配置的方法,但我不能返回一個 「新的重定向(loginPage)」 ...

任何人有一個想法該怎麼做?

+0

你不能注入路由器,然後定位到路線? –

回答

3

下面是一個例子:

import { HttpClient } from 'aurelia-fetch-client'; 
import { inject } from 'aurelia-framework' 
import { Router } from 'aurelia-router' 

@inject(HttpClient, Router) 
export class UserService { 
    http 
    router 

    constructor(http, router) { 
    this.http = http 
    this.router = router 

    this.http.configure(config => { 
     var self = this; 
     config 
     .withInterceptor({ 
     responseError(response) { 
      if (response.status === 401) { 
      self.router.navigateToRoute('login') 
      } 
      return response; // you can return a modified Response 
     }, 
     }); 
    }); 
    } 
相關問題