Jay*_*Jay 4 parameters router angular
我有一个列表页面和一个详细信息页面.从列表页面中选择项目将路由到详细信息页面.
路由模块:
const itemRoutes: Routes = [
{ path: 'item', component: ItemListComponent},
{ path: 'item/:id', component: ItemDetailComponent }
];
Run Code Online (Sandbox Code Playgroud)
列表组件:
constructor( private route: ActivatedRoute, private router: Router) {}
goToDetail(item) {
this.router.navigate(['item'], {id: item.id});
}
Run Code Online (Sandbox Code Playgroud)
问题:选择项目会转到此URL: http:// localhost:3000/item/2
但浏览器显示"未找到"错误.
在控制台上,我看到了这个:
:3000/item/2:1 GET http://localhost:3000/item/2 404 (Not Found)
Navigated to http://localhost:3000/item/2
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Jay*_*Jay 12
我认为问题是我在列表页面的html上有这个:
<a href="item/{{item.id}}">{{item.id}}</a>
Run Code Online (Sandbox Code Playgroud)
我改成了:
<a [routerLink]="['/item', item.id]">{{item.id}}</a>
Run Code Online (Sandbox Code Playgroud)
并从组件中删除它:
goToDetail(item) {
this.router.navigate(['item'], {id: item.id});
}
Run Code Online (Sandbox Code Playgroud)
这有效!
| 归档时间: |
|
| 查看次数: |
22448 次 |
| 最近记录: |