是否可以从多个视图模型配置路由器?
像下面这样的东西?
class App {
...
constructor(router) {
this.router.configure(config => {
config.map([{
route: 'home',
moduleId: 'home',
nav: true
}])
})
}
}
Run Code Online (Sandbox Code Playgroud)
更改其他视图模型中的路由器配置:
class SomeOtherPage {
...
constructor(router) {
this.router.configure(config => {
config.map([{
route: 'someOtherPage',
moduleId: 'someOtherPage',
nav: true
}])
})
}
}
Run Code Online (Sandbox Code Playgroud)
来自aurelia的文档:
与Aurelia的所有内容一样,我们对会议提供强有力的支持.因此,您实际上可以选择动态路由而不是预先配置所有路由.以下是配置路由器的方法:
router.configure(config => {
config.mapUnknownRoutes(instruction => {
//check instruction.fragment
//set instruction.config.moduleId
});
});
Run Code Online (Sandbox Code Playgroud)
你所要做的就是设置config.moduleId属性,你就可以了.您还可以从mapUnknownRoutes返回一个promise,以便异步确定目标.
此外,路由器有一个addRoute方法可以在您的情况下工作,以便以后添加路由.