【问题标题】:Rebuilding routes after refresh刷新后重建路线
【发布时间】:2016-01-27 21:08:13
【问题描述】:

我在 Aurelia 中处理一些路由问题时遇到了一些麻烦。

当用户访问我的应用时,如果他们之前已经认证,我想将他们重定向到着陆页。如果没有,请直接进入登录页面。

我的经过身份验证的用户重定向工作正常(app.js -> login.js -> setupnav.js -> 登录页面)。

我现在遇到的问题是:

  • 当用户刷新页面 (http://localhost:8088/aurelia-app/#/landing) 时,landing 路由不再存在,并在控制台中抛出错误 (ERROR [app-router] Error: Route not found: /landing(…))。如果找不到路线,我想将用户引导至login

有人知道如何将用户从丢失的路由重定向到我的login 页面吗?

也欢迎任何关于我如何设置路由的 cmets。

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

【问题讨论】:

    标签: javascript aurelia aurelia-auth


    【解决方案1】:

    要将未知路由映射到特定页面,请使用mapUnknownRoutes 功能:

    configureRouter(config, router) {
      ...
      config.mapUnknownRoutes(instruction => {
        return 'login';
      });
    }
    

    也就是说,将所有与身份验证相关的逻辑排除在路由之外可能更容易,而是使用setRoot 根据用户的身份验证状态设置适当的根模块。

    标准的main.js 如下所示:

    ma​​in.js

    export function configure(aurelia) {
      aurelia.use
        .standardConfiguration()
        .developmentLogging();
    
      aurelia.start().then(a => a.setRoot());
    }
    

    你可以把逻辑改成这样:

    ma​​in.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

    【讨论】:

    • config.mapUnknownRoutes 正是我想要的,谢谢!我正在使用aurelia-auth 插件,所以我认为我不能在main.js.. 中使用它。
    • 您应该将router.navigate('login'); 更改为return 'login';,否则您将在控制台中收到错误消息。我尝试编辑您的帖子,但被拒绝:P
    • 我认为它被拒绝了,因为您的编辑中有错字。让我看看我能不能解决它。
    猜你喜欢
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2019-03-12
    • 2021-10-27
    • 2015-11-16
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多