我有一個看起來像這樣的路由器鏈接:
<li *ngFor = "let union of activeUnions" [routerLink] = "['union',union.id]">
哪個是使用路由的路由:
const routes: Routes = [
{
path: '',
component: IndexPage
},
{
path:"new",
component:NewPage
},
{
path:"union/:id",
component:UnionPage
}
];
這將我發送到所需的組件,但是當我嘗試獲取這樣的 id 時
ngOnInit() {
let id = this.$common.$route.snapshot.paramMap
console.log(id);
}
我得到一個空物件而不是包含來自 url 的 id 的物件,有什么問題嗎?
uj5u.com熱心網友回復:
看看您是否可以通過以下方式獲得所需的id:
...
import { ActivatedRoute } from '@angular/router';
...
constructor(
...
private activatedRoute: ActivatedRoute
...
) {}
ngOnInit(): void {
const id = Number(this.activatedRoute.snapshot.paramMap.get('id'));
console.log(id);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/413254.html
標籤:
上一篇:Angular組件創建最佳實踐
