【发布时间】:2017-02-09 13:47:32
【问题描述】:
我有 3 层嵌套路由器,定义如下:
app.js
configureRouter(config, router) {
this.router = router;
config.title = 'EagleWeb';
var step = new AuthorizeStep(this.core);
config.addAuthorizeStep(step);
config.map([
{ route: '', redirect: 'home' },
{ route: 'home', name: 'home', moduleId: 'home/home' },
{ route: 'users', name: 'users', moduleId: 'users/user-home' },
{ route: 'accounting', name: 'accounting', moduleId: 'accounting/accounting' }
]);
}
accounting/accounting.js
configureRouter(config, router) {
config.map([
{ route: '', redirect: 'home' },
{ route: 'home', name: 'accounting-home', moduleId: 'accounting/accounting-home' },
{ route: 'accounts', name: 'accounting-accounts', moduleId: 'accounting/accounts/accounts' },
{ route: 'entries', name: 'accounting-entries', moduleId: 'accounting/entries/entries' }
]);
this.router = router;
}
accounting/accounts/accounts.js
configureRouter(config, router) {
config.map([
{ route: '', redirect: 'list' },
{ route: 'list', name: 'accounting-account-list', moduleId: 'accounting/accounts/account-list' },
{ route: 'view/:id', name: 'accounting-account-view', moduleId: 'accounting/accounts/account-view' }
]);
this.router = router;
}
我想导航到第三级,它可以使用普通链接标签或直接输入 URL(例如<a href="#/accounting/accounts/view/2">),但以下命名路由的方法都不起作用:
- (来自 app.html,级别 1 尝试访问级别 3)
<a route-href="route: accounting-account-list">Account List</a> - (来自 app.html,级别 1 尝试访问级别 3)
<a route-href="route: accounting-account-view; params.bind: {id: 2}">Account #2</a> - (来自accounting-home.js,2级试图访问3级)
this.router.navigateToRoute('accounting-account-view', {id: 2}); - (来自accounting-home.js,2级试图访问3级)
this.router.navigateToRoute('accounting/accounting-accounts/accounting-account-view', {id: 2});
对于 #1-2,当应用加载时,我会收到类似 Error: A route with name 'accounting-account-list' could not be found. Check that name: 'accounting-account-list' was specified in the route's config. 的控制台日志错误,并且链接已失效。
对于 #3-4,当页面加载和每次导航时,我都会收到类似 Uncaught Error: A route with name 'accounting-account-view' could not be found. 的控制台日志错误。
我做错了什么?我需要做一些特殊的事情来访问不同路由器级别的路由吗?
附带问题:我需要在每个视图模型中使用import {Router} from 'aurelia-router';,一些,还是不需要?需要注射吗?
【问题讨论】:
-
我认为你需要简化你的路由方案!但说真的,如果没有其他人,我会在稍后尝试回答。
-
scottwhittaker.net/aurelia/2016/06/12/aurelia-router-demo.html 有一个很好的演示,但它没有解决命名路由的使用。
-
谢谢!非常感谢您的帮助!
-
@AshleyGrant,你会建议我尝试只折叠成两层路由器吗?
-
如果那里只有这三条路线,我会倾向于这样做。不过,这真的取决于全貌。我没有忘记这个问题。我最近很忙。
标签: aurelia