我在奥雷利亚工作有点麻烦。
当用户转到我的应用程序时,如果他们以前有authenticated,,我想将他们重定向到登陆页面。如果没有,直接到登录页面。
我有经过身份验证的用户重定向工作状态(app.js -> login.js -> setupnav.js ->着陆页面)。
我现在的问题是:
http://localhost:8088/aurelia-app/#/landing)时,landing路由不再存在,控制台(ERROR [app-router] Error: Route not found: /landing(…))中会抛出一个错误。如果找不到路由,我想将用户引导到login。有人知道如何将用户从丢失的路由重定向到我的login 页面吗?
另外,任何关于我如何设置路由的评论都是受欢迎的。
app.js
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
import {FetchConfig} from 'aurelia-auth';
import {AuthorizeStep} from 'aurelia-auth';
import {AuthService} from 'aurelia-auth';
@inject(Router,FetchConfig, AuthService )
export class App {
constructor(router, fetchConfig, authService){
this.router = router;
this.fetchConfig = fetchConfig;
this.auth = authService;
}
configureRouter(config, router){
config.title = 'VDC Portal';
config.addPipelineStep('authorize', AuthorizeStep); // Add a route filter to the authorize extensibility point.
config.map([
{ route: ['','login'], name: 'login', moduleId: './login', nav: false, title:'Login' },
{ route: '', redirect: "login" },
{ route: 'setupnav', name: 'setupnav', moduleId: './setupnav', nav: false, title:'setupnav' , auth:true}
]);
this.router = router;
}
activate(){
this.fetchConfig.configure();
}
created(owningView: View, myView: View, router){
/* Fails to redirect user
if(this.auth.isAuthenticated()){
console.log("App.js ConfigureRouter: User already authenticated..");
this.router.navigate("setupnav");
}
*/
}
}login.js
import {AuthService} from 'aurelia-auth';
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
@inject(AuthService, Router)
export class Login{
constructor(auth, router){
this.auth = auth;
this.router = router;
if(this.auth.isAuthenticated()){
console.log("Login.js ConfigureRouter: User already authenticated..");
this.router.navigate("setupnav");
}
};
heading = 'Login';
email='';
password='';
login(){
console.log("Login()...");
return this.auth.login(this.email, this.password)
.then(response=>{
console.log("success logged");
console.log(response);
})
.catch(err=>{
console.log("login failure");
});
};
}重定向到:
setupnav.js
import {Router} from 'aurelia-router';
import {inject} from 'aurelia-framework';
@inject(Router)
export class Setupnav{
theRouter = null;
constructor(router){
console.log("build setupnav. router:" + this.theRouter);
this.theRouter = router
};
activate()
{
this.theRouter.addRoute( { route: 'landing', name: 'landing', moduleId: 'landing', nav: true, title:'Integration Health' , auth:true});
this.theRouter.addRoute( { route: 'tools', name: 'tools', moduleId: 'tools', nav: true, title:'Integration Tools' , auth:true});
this.theRouter.refreshNavigation();
this.theRouter.navigate("landing");
}
}发布于 2016-01-26 18:07:07
若要将未知路由映射到特定页面,请使用mapUnknownRoutes特性:
configureRouter(config, router) {
...
config.mapUnknownRoutes(instruction => {
return 'login';
});
}也就是说,可能更容易将所有与auth相关的逻辑排除在路由之外,而是使用setRoot根据用户的auth状态设置适当的根模块。
标准的main.js如下所示:
main.js
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.start().then(a => a.setRoot());
}您可以将逻辑更改为如下所示:
main.js
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.start().then(() => {
if (userIsAuthenticated) {
return aurelia.setRoot('app');
}
if (userPreviouslyAuthenticated) {
return aurelia.setRoot('login');
}
return aurelia.setRoot('landing');
});
}在上面的示例中,app模块是配置路由的唯一模块。一旦用户成功登录,login模块将是一个名为setRoot('app')的登录页面。当用户单击链接/按钮时,landing页面将调用setRoot('login')。
下面是一个可能有用的相关问题的答案:https://stackoverflow.com/a/33458652/725866
https://stackoverflow.com/questions/35019879
复制相似问题