使用Aurelia重定向类
我已将授权管道步骤添加到我的路由器。一切正常,但当我使用使用Aurelia重定向类,aurelia,Aurelia,我已将授权管道步骤添加到我的路由器。一切正常,但当我使用重定向类将用户指向登录页面时,它使用URL作为参数。我希望像使用Router.navigateToRoute()一样传入路由名称。这可能吗 @inject(AuthService) class AuthorizeStep { constructor (authService) { this.authService = authService; } run (navigationInstruction
重定向
类将用户指向登录页面时,它使用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();
}
}
通过谷歌搜索,我找到了
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'));