angular - Angular Routing not working when adding ID as a parameter in URL
问题描述
new to Angular here. I have the following in my app.module.ts
RouterModule.forRoot()
{
...
{
path : "posts/:id",
component: PostprofileComponent
},
{
path : "posts",
component: PostsComponent
},
...
}
The following is redirecting correctly to PostprofileComponent.
However this link does not... it redirects to PostsComponent.
http://localhost:4200/posts?id=2
Shouldn't they behave the same? What am i doing wrong?
解决方案
在你的RouterModule
:
RouterModule.forRoot()
{
...
{
path : "posts/details/:id",
component: PostprofileComponent
},
{
path : "posts/details",
component: PostprofileComponent
},
{
path : "posts",
component: PostsComponent
},
...
}
使用查询参数的用法示例:(更多信息在这里)
this.router.navigate(['/posts/details'], { queryParams: { id: '2' } });
网址:http://localhost:4200/posts/details?id=2 返回=> PostprofileComponent
使用路由参数的用法示例:
this.router.navigate(['/posts/details', '2']);
网址:http://localhost:4200/posts/details/2 返回=> PostprofileComponent
然后http://localhost:4200/posts 返回 => PostsComponent
推荐阅读
- r - R for-loop for ftp地址中的变量
- html - 由于文本内容,div 中的按钮呈现方式不同
- terraform - Terraform iam_policy_document - 差异。资源与本金
- angular - 如何使用 ngx-pagination 对 div 进行分页
- webrtc - NACK 在 webrtc 中是如何工作的?
- c# - 从 c# 到 c 应用程序的串行端口通信问题
- android - AppWidget Imageview updatePeriodMillis
- c# - 无法从自定义域中解开 COM 实例
- javascript - 如何将点的对象从js传递到java作为列表?
- python - pyOpenGL 三角形没有被绘制