angular - Angular 组件路由中的挑战
问题描述
我已经定义了一个子组件路由。
- 我无法从 routerLink 导航到默认路由。
- 父路径具有“:id”,因此对于子路由,它仅使用前缀“:id”。
路线
{
path: ':id', component: InterviewComponent,
children: [
{
path: '',
component: InterviewQuestionsComponent
},
{
path: ':id/interview',
component: InterviewGridComponent
}
]
}
锚标签
<ul class="list-unstyled components">
<li>
<a routerLink=""><i class='interviewQuestions'></i> <span style="cursor: pointer;">Interview Questions</span></a>
</li>
<li>
<a routerLink=":id/interview"><i class='interviews'></i> <span style="cursor: pointer;">Interviews</span></a>
</li>
</ul>
<router-outlet></router-outlet>
- 我无法使用 'routerLink=""' 导航到默认路由器
- 仅适用于“路径:':id/interview'”的路线
解决方案
There are couple of things that I observed here,
if I understand you correctly what you are trying to do is,
/1 should render InterviewQuestionsComponent
/1/interview should render InterviewGridComponent
right? correct me if I am wrong.
if this is what you want then modify your routes with below,
{
path: ':id', component: InterviewComponent,
children: [
{
path: '',
component: InterviewQuestionsComponent
},
{
path: 'interview',
component: InterviewGridComponent
}
]
}
- To navigate to each of these components, you will need 'id', which is a path param, so what you have done in routerLink is not correct.
modify it like below,
<ul class="list-unstyled components">
<li>
<a routerLink="1"><i class='interviewQuestions'></i> <span style="cursor: pointer;">Interview Questions</span></a>
</li>
<li>
<a routerLink="1/interview"><i class='interviews'></i> <span style="cursor: pointer;">Interviews</span></a>
</li>
</ul>
Here I have hardcoded '1' in the path, you can make it dynamic using Angular bindings. Here's a working example on stackblitz.
推荐阅读
- python - 查找所有不包含特定字符串的组合
- reactjs - 用于 AEM 6.1 和 6.2 版本的 AEM + React 集成
- css - 带系统网格的布局
- r - 根据第一个矩阵的行上的条件在 R 中形成一个新矩阵
- javascript - 在一系列时刻中查找 moment.js 对象?
- react-native - 带有两个按钮的平面列表单项
- sql - SQL:用于两级聚合的 INNER JOIN
- python - 为什么不能在 __init__ 中调用对象属性?
- javascript - 如何在 react-navigation 5 中设置背景颜色
- ios - 如何使用 Swift 在 iOS 中将音频 mp3 文件转换为原始音频类型?