2

是否可以从多个视图模型配置路由器?

像下面这样的东西?

class App {
  ...
  constructor(router) {
    this.router.configure(config => {
      config.map([{
        route: 'home',
        moduleId: 'home',
        nav: true
      }])
    })
  }
}

在其他视图模型中更改路由器配置:

class SomeOtherPage {
  ...
  constructor(router) {
    this.router.configure(config => {
      config.map([{
        route: 'someOtherPage',
        moduleId: 'someOtherPage',
        nav: true
      }])
    })
  }
}
4

1 回答 1

8

来自aurelia 文档

与 Aurelia 中的所有内容一样,我们强烈支持约定。因此,您实际上可以选择动态路由,而不是预先配置所有路由。以下是您如何配置路由器来做到这一点:

router.configure(config => {
  config.mapUnknownRoutes(instruction => {
    //check instruction.fragment
    //set instruction.config.moduleId
  });
});

您所要做的就是设置 config.moduleId 属性,一切顺利。您还可以从 mapUnknownRoutes 返回一个承诺,以异步确定目的地。

此外,路由器有一种addRoute方法应该适用于您的情况,以便稍后添加路由。

于 2015-02-12T11:26:30.517 回答