Aurelia - 在具有不同路由配置的应用程序根之间切换

标签 aurelia

更新

这个问题可能与mu问题有关吗?
https://github.com/aurelia/framework/issues/400

我有一个 Aurelia具有两个不同根的应用程序,一个用于登录用户,另一个用于匿名用户。

我在其他 Aurelia 应用程序中实现了应用程序根目录的更改 based on the approach in this answer .当 login模块是一个“孤立”的模块,没有额外的路线,但我现在很难让它工作。

index.js - 匿名用户的根目录

import {inject, useView, Aurelia} from "aurelia-framework";
import AuthService from "./services/auth-service";

@useView("app.html")
@inject(AuthService)
export class Index {   
    constructor(authService) {
        this.auth = authService;
    }

    configureRouter(config, router) {
        config.title = "Super Secret Project";
        config.options.pushState = true;
        config.map([
            { route: ["","home"], moduleId: "home", nav: true, title: "Beginscherm" },
            { route: "over", moduleId: "about", nav: true, title: "Over" },
            { route: "inloggen", moduleId: "account/login", nav: false, title: "Inloggen" }
        ]);

        this.router = router;        
    }
}

ic-app.js - 登录用户的根目录
import {useView, inject} from "aurelia-framework";
import {RequestStatusService} from "./services/request-status-service";
import AuthService from "./services/auth-service";

@useView("app.html")
@inject(RequestStatusService, AuthService)
export class App {
    constructor(requestStatusService, authService) {
        this.requestStatusService = requestStatusService;
        this.auth = authService; // we use it to bind it to the nav-bar-top
    }

    configureRouter(config, router) {
        config.title = "Super Secret Project";
        config.options.pushState = true;
        config.map([
            { route: ["", "selecteer-school"], moduleId: "ic/select-school", nav: false, title: "Selecteer School" },
            { route: "dashboard", moduleId: "ic/dashboard", nav: true, title: "Beginscherm" },
        ]);

        this.router = router;
    }
}

auth-service.js 上的登录代码
logIn(userData, rememberMe = false) {
    this.requestStatusService.isRequesting = true;
    return this.http
        .fetch("/token", { method: "POST", body: userData })
        .then((response) => response.json())
        .then(response => {
            if (response.access_token) {
                this.setAccessToken(response.access_token, response.userName, rememberMe);
                this.app.setRoot("ic-app");
            }
        });
}

和...

注销 auth-service.js 中的代码
logOff() {
    AuthService.clearAccessToken();
    this.app.setRoot("index");
}

问题

设置不同的应用程序根目录按预期工作,问题是我希望新的应用程序根目录自动导航到 新根的默认路由 , 位它试图加载它当时的路线setRoot(...)叫做。

举例说明,
  • 我在登录页面。 current route: /inloggen
  • 我点击登录按钮。 app.setRoot("ic-app") is called
  • 新的根被加载; configureRouter在 ic-app.js 中被调用,然后...
  • 控制台错误:Route not found: /inloggen

  • 新的根试图保持在同一个 /inloggen路由,但我希望它加载或导航到该应用程序根目录的默认路由。
    注销时也会发生同样的情况。

    更改 root 后如何强制应用导航到默认路由?

    最佳答案

    我做得很好,我回答了更多关于如何在这个stackoverflow线程中的信息:

    Aurelia clear route history when switching to other app using setRoot

    基本上做到以下几点

    this.router.navigate('/', { replace: true, trigger: false });
    this.router.reset();
    this.router.deactivate();
    
    this.aurelia.setRoot('app');
    

    关于Aurelia - 在具有不同路由配置的应用程序根之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38120388/

    相关文章:

    typescript - Aurelia Typescript 和 DI

    javascript - 如何在 Aurelia 中对表单验证进行单元测试

    javascript - 无法读取未定义的属性 'WebAuth'(Auth0 错误)

    javascript - 图表选项不与图表 js 一起使用

    javascript - Aurelia - repeat.for 按值转换器排序在 IE 11 中失败

    jquery - 在 Typescript 类中访问 jquery 验证变量

    typescript - 需要有关创建 typescript 公共(public)类型定义的策略的指导

    aurelia - 如何通过环境在 aurelia 中设置常量

    Javascript 不是从 Typescript 生成的

    aurelia - 可能在 Aurelia 中等待自定义元素中的 promise 在执行继续之前完成?