简体   繁体   English

Aurelia:子路由器路由显示在app.html的“main”导航栏和子视图中 <router-view> 元件?

[英]Aurelia: child router routes display in “main” nav-bar and child view in app.html <router-view> element?

We want to have a sidebar menu and a "main" area. 我们想要一个侧边栏菜单和一个“主要”区域。 Depending on how you navigate, the sidebar's menu items will change, and a new view will be loaded into the "main" area. 根据您的导航方式,侧边栏的菜单项将更改,并且新视图将加载到“主”区域。

I've created app.html with a <router-view> element, and a nav-bar.html that can display the main router's navigation. 我创建了一个带有<router-view>元素的app.html,以及一个可以显示主路由器导航的nav-bar.html。 Let's say that I initially have "Administration" and "Reports" as routes (and therefore menu items). 假设我最初将“管理”和“报告”作为路径(以及菜单项)。 When a user clicks on "Administration", I'd like the menu to update to display child routes (say "Users" and "Settings") and have the admin view-model display in the app.html's <router-view> . 当用户点击“管理”时,我希望菜单更新以显示子路径(例如“用户”和“设置”),并在app.html的<router-view>显示管理视图模型。

Initially I tried to create a child router, but then I have to have a new <router-view> inside of admin.html (the page won't even load without this). 最初我尝试创建一个子路由器,但后来我必须在admin.html中有一个新的<router-view> (如果没有这个页面甚至不会加载)。 Instead, I want the admin.html view to display inside the <router-view> of app.html, and for the child routes to replace the "main" routes in the nav-bar menu. 相反,我希望admin.html视图显示在app.html的<router-view>内,以及子路径以替换导航栏菜单中的“主”路由。

In app.js I have the following router config: 在app.js我有以下路由器配置:

this.router.configure((config) => {
    config.title = "Welcome";
    config.map([
        { route: "", moduleId: "welcom", nav: false, title: "Welcome" },
        { route: "reports", moduleId: "reports", nav: true, title: "Reports" },
        { route: "admin", moduleId: "users", nav: true, title: "Administration" },
    ]);
});

In users.js, I have this code: 在users.js中,我有这个代码:

this.router.configure((config) => {
    config.title = "Users";
    config.map([
        { route: "", moduleId: "users", nav: true, title: "Users" },
        { route: "settings", moduleId: "settings", nav: true, title: "Settings" },
    ]);
});

Initially, the menu should be: 最初,菜单应该是:
- Administration - 行政
- Reports - 报告

and "welcome.html" should be the view in the <router-view> (the default route is 'welcome'). 并且“welcome.html”应该是<router-view> (默认路由是'welcome')。

When the user clicks (navigates to) "Administration", the menu should refresh to be: 当用户单击(导航到)“管理”时,菜单应刷新为:
- Users - Settings - 用户 - 设置

with "users.html" in the <router-view> . <router-view>使用“users.html”。

However, in order to get this to work at all I need to have a further <router-view> in "users.html" and that's not really what I want (I want the view to load in the app.html's <router-view> ). 但是,为了使这一点工作,我需要在“users.html”中进一步使用<router-view> ,这不是我想要的(我希望视图加载到app.html的<router-view> )。

Is there a way to achieve this in Aurelia? 有没有办法在奥里利亚实现这一目标? I've even tried injecting the parent router into the Admin constructor (using Parent.of(router) binding) and then router.addRoute() . 我甚至尝试将父路由器注入Admin构造函数(使用Parent.of(router)绑定),然后router.addRoute() The route gets added, but the menu doesn't update (even though it's data bound). 路径被添加,但菜单不更新(即使它的数据绑定)。

I created a sample using Aurelia that implements a hierarchical menu structure using a left-side menu. 我使用Aurelia创建了一个示例,该示例使用左侧菜单实现了分层菜单结构。

Here are notes about the sample project 以下是有关示例项目的说明

You can run the sample online to test it out 您可以在线运行示例以进行测试

多级菜单示例

The multi-level menu sample shows how you can quickly create a hierarchical menu when building an Aurelia SPA website. 多级菜单示例显示了在构建Aurelia SPA网站时如何快速创建分层菜单。

The multi-level menu helps users navigate through a hierarchy of pages. 多级菜单可帮助用户浏览页面层次结构。

It is easy to configure the routes and the hierarchy as shown below: 可以轻松配置路由和层次结构,如下所示:

import aur = require("aurelia-router");
import mlmps = require("./MultiLevelMenuPipelineStep");

export class App {
    static inject = [aur.Router];

    constructor(public router: aur.Router) {
        this.router.configure((config) => {
            config.title = "Aurelia VS/TS";
            config.addPipelineStep("modelbind", mlmps.MultiLevelMenuPipelineStep);
            config.map([
                { route: ["", "home"], moduleId: "views/home", nav: true, title: "home", settings: { level: 0, show: true } },
                { route: ["item-1"], moduleId: "views/item-1", nav: true, title: "item 1", settings: { level: 0, show: true } },
                { route: ["item-1-1"], moduleId: "views/item-1-1", nav: true, title: "item 1.1", settings: { level: 1, show: false } },
                { route: ["item-1-2"], moduleId: "views/item-1-2", nav: true, title: "item 1.2", settings: { level: 1, show: false } },
                { route: ["item-2"], moduleId: "views/item-2", nav: true, title: "item 2", settings: { level: 0, show: true } },
                { route: ["item-2-1"], moduleId: "views/item-2-1", nav: true, title: "item 2.1", settings: { level: 1, show: false } },
                { route: ["item-2-2"], moduleId: "views/item-2-2", nav: true, title: "item 2.2", settings: { level: 1, show: false } },
                { route: ["item-2-2-1"], moduleId: "views/item-2-2-1", nav: true, title: "item 2.2.1", settings: { level: 2, show: false } },
                { route: ["item-2-2-2"], moduleId: "views/item-2-2-2", nav: true, title: "item 2.2.2", settings: { level: 2, show: false } },
                { route: ["item-2-3"], moduleId: "views/item-2-3", nav: true, title: "item 2.3", settings: { level: 1, show: false } }
            ]);
        });
    }
}

The level property is used to establish the hierarchy. level属性用于建立层次结构。 The show property controls the visibility of the route in the menu. show属性控制菜单中路径的可见性。 The router navigation pipeline step looks at the target navigation and sets the route visiblity accordingly. 路由器导航管道步骤查看目标导航并相应地设置路径可见性。 The navigation helper provides a button to navigate up a level from the current route, and invokes the corresponding navigation commmand to the router. 导航助手提供了一个按钮,用于从当前路线向上导航一个级别,并调用相应的导航命令到路由器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM