使用Aurelia Redirect类

pow*_*uoy 4 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

pow*_*uoy 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

import { RedirectToRoute } from 'aurelia-router';
Run Code Online (Sandbox Code Playgroud)

...

return next.cancel(new RedirectToRoute('login'));
Run Code Online (Sandbox Code Playgroud)