使用Aurelia Redirect类

时间:2016-08-07 15:01:59

标签: aurelia aurelia-router

我已向路由器添加了授权管道步骤。一切正常,但是当我使用Redirect类将用户指向登录页面时,它会将URL作为其参数。如果我使用Router.navigateToRoute(),我更愿意传递路线名称。这可能吗?

@inject(AuthService)
class AuthorizeStep {
    constructor (authService) {
        this.authService = authService;
    }

    run (navigationInstruction, next) {
        if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
            if (!this.authService.isLoggedIn) {
                return next.cancel(new Redirect('url-to-login-page')); // Would prefer to use the name of route; 'login', instead.
            }
        }

        return next();
    }
}

1 个答案:

答案 0 :(得分:7)

在一些谷歌搜索后,我找到了Router.generate()方法,该方法采用路由器名称(和可选参数)并返回URL。我现在已将授权步骤更新为以下内容:

@inject(Router, AuthService)
class AuthorizeStep {
    constructor (router, authService) {
        this.router = router;
        this.authService = authService;
    }

    run (navigationInstruction, next) {
        if (navigationInstruction.getAllInstructions().some(i => i.config.auth)) {
            if (!this.authService.isLoggedIn) {
                return next.cancel(new Redirect(this.router.generate('login')));
            }
        }

        return next();
    }
}

编辑:经过一些谷歌搜索后,我找到了RedirectToRoute类;

import { RedirectToRoute } from 'aurelia-router';

...

return next.cancel(new RedirectToRoute('login'));