如果服务器 returns 401 响应,则重定向到登录页面

Redirect to login page if server returns 401 response

如果是 401 响应,我如何拦截服务器端点响应并将 aurelia 应用程序重定向到登录页面?

我尝试了 "withInterceptor(responseError() {...})" aurelia-fetch-client 配置的方法,但我无法 return "new Redirect(loginPage)"...

有人知道怎么做吗?

这是一个例子:

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

@Moustachiste 用这种方法解决了同样的问题:

注入一定要这样

constructor(
// private _router: Router
@lazy(Router) private _router: () => Router
)