【发布时间】:2026-02-07 11:40:01
【问题描述】:
我正在尝试从一个子路径导航到另一个子路径,但我不断收到Route not found。我的主要问题:如何在子视图之间导航?
下面是代码,下面我还有其他问题。
应用模式-查看 应用类:
export class App {
configureRouter(config, router) {
config.title = 'My Site';
config.map([
{ route: ['','job-view'], name: 'jobView', moduleId: './job-view', nav: true, title:'Jobs'},
{ route: ['services'], name: 'services', moduleId: './services', nav: true, title:'Services'}
]);
this.router = router;
this.router.refreshNavigation();
}
}
Q.2:如果router 始终可以从aurelia-router 访问,为什么我们需要在此处保存它?
应用页面:
<template>
<require from='./nav-bar'></require>
<nav-bar router.bind="router"></nav-bar>
<div class="container">
<router-view></router-view>
</div>
</template>
好的,现在我们已经定义了根页面视图和导航,让我们定义job-view MV。
JobView 类:
export class JobView {
configureRouter(config, router) {
config.map([
{ route: ['','jobs'], name: 'jobs', moduleId: './jobs', nav: false, title:'Jobs', settings:{icon:'glyphicon glyphicon-align-justify'} },
{ route: ['job/:id'], name: 'job', moduleId: './job', nav: false, title:'Job Details'}
]);
this.router = router; //WHY SAVE THIS?
this.router.refreshNavigation();
}
}
JobView 页面:
<template>
<router-view></router-view>
</template>
现在,这里是子视图。我的假设是发生的路由应该与job-view 相关。理想情况下,这就是我想要的。
Jobs Class (为简洁起见删除了一堆代码):
import {Router} from 'aurelia-router';
@inject(Router)
export class Jobs {
constructor(router) {
this.router = router;
}
toJob(id) {
// this.router.navigateToRoute("job", {id:id}); // ERROR - ROUTE NOT FOUND
this.router.navigate("#/job-view/job/".concat(id)); // THIS WORKS
}
}
Q.3:我已经看到router.navigateToRoute 和router.navigate 都被引用了,但没有说明何时使用它们或有什么区别,而且文档也没有解释.应该使用哪个? Docs
职位页面:
jobs.html 的详细信息无关紧要,因此不在此处列出。
最后,job 视图:
工作类别:
与job.js 无关,因此不列出代码。最多我可以导航回jobs,但这在页面下方处理。
职位页面:
<!-- a bunch of html //-->
<!-- HOW TO USE ROUTER IN HTML, NOT BELOW URL HREF? //-->
<a href="#/jobs">Print Jobs</a>
<!-- a bunch of html //-->
Q.4:同样,我想路由到相对位置,即使在 HTML 页面中也是如此。上面的不行,那我应该用什么?
在类似的问题中有一个proposed answer,但是将job-view 注入job 并使用job-view 保存的路由器也不起作用。
顺便说一句,如果我手动导航到http://localhost:3000/#/job-view/job/3,页面加载正常,所以很清楚路由器的某些内容。
模组注意事项: 在How to access child router in Aurelia? 上提出了类似的问题,但没有得到有效的解决方案。
【问题讨论】:
标签: aurelia