Setting a “related” route as active in Aurelia

允我心安 提交于 2019-12-04 09:57:36

If I understand the question correctly, you have a "Work" link in your nav-bar, which launches the list of "work". Then when you click on an item in your work list you want the "Work" link in the nav-bar to remain active when the work "detail" screen is activated.

Here's the approach that I used in this application:

In app.js, configure the "work" route:

export class App {
  configureRouter(config, router) {
    config.title = 'Aurelia';
    config.map([
      { route: '', redirect: 'work' },
      { route: 'work', moduleId: './work/work-section', nav: true, title: 'Work' },
      // ... routes for other top-level sections ...
    ]);
    this.router = router;
  }
}

Then add a "work" folder to your project. This folder will contain the "work" related views and view-models.

In the work folder, add "work-section.js":

/**
* The shell for the work section of the application.  Will display either
* the work-list or work-detail page.
*/
export class WorkSection {
  configureRouter(config, router) {
    config.map([
      { route: '',    moduleId: './work-list',   nav: false, title: '' },
      { route: ':id', moduleId: './work-detail', nav: false, title: '' },
    ]);
  }
}

Next add "work-section.html". It's just a <router-view>:

<template>
  <router-view></router-view>
</template>

Finally, add your work-list.js + .html and work-detail.js + .html to the "work" folder. When clicking on the "work" link in the nav-bar, the work-list will be displayed by default. When you drill into an item in the work-list, the "work" link in the nav-bar will remain active.

Check out the sample application. It has three top-level sections (orders, customers and employees). Each section uses this approach. The code is here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!