繁体   English   中英

Aurelia子路由器重定向到特定路由

[英]Aurelia Child Router redirect to specific route

我有一个主App路由器和多个子路由器。 我想选择指定从父路线导航时打开的子路线。

父路由器:

configureRouter(config, router) {
  this.router = router;
  config.map([
    { route: ['','home'],      name: 'home',         moduleId: 'home/home' },
    { route: 'students/:id?',  name: 'students',     moduleId: 'students/students' },
    { route: 'staff',          name: 'staff',        moduleId: 'staff/staff' }
  ]);
}

学生专用儿童路由器:

export class Students {

  configureRouter(config, router) {
    config.map([
      { route: ['', 'home'],    name: 'student-home',           moduleId: 'students/students-home' },
      { route: 'list',          name: 'student-list',           moduleId: 'students/student-list' },
      { route: 'profile/:id',   name: 'student-profile',        moduleId: 'students/profile/overview' },
      { route: 'lockers',       name: 'student-lockers',        moduleId: 'students/lockers/home' }
    ]);
    this.router = router;
  }

  activate(params) {
    if (params.id) {
      console.log("Going straight to a student record for: ", params);
      this.router.navigateToRoute('student-profile', {id: params.id});
    }
  }
}

上面的情况(在activate中使用navigateToRoute() )不起作用,我也不确定这是最好的方法。 如果我包含id参数,如何选择从主应用路由器直接导航到学生资料屏幕?

我放弃了在子路由器上使用命名路由。 如果有人比我更了解他们,我会很好奇。 但是,我发现仅从应用程序的任何部分使用URL路由就可以很好地工作。

this.router.navigate('#/students/profile/' + record.id);

您无需在子路径中使用主动。 Aurelia路由器将自动转到您的子路线。

export class Students {

  configureRouter(config, router) {
    config.map([
      { route: ['', 'home'],    name: 'student-home',           moduleId: 'students/students-home' },
      { route: 'list',          name: 'student-list',           moduleId: 'students/student-list' },
      { route: 'profile/:id',   name: 'student-profile',        moduleId: 'students/profile/overview' },
      { route: 'lockers',       name: 'student-lockers',        moduleId: 'students/lockers/home' }
    ]);
    this.router = router;
  }
}

删除active,然后在您的模块“ students / profile / overview”中调用active(params)以从api获取学生,或者您可以使用已提供的params在这里进行任何操作。

暂无
暂无

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

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