我在尝试获取:id参数
时遇到问题这是我的路线:
{
path: 'user',
component: UserComponent,
children: [
{
path: ':id',
component: UserComponent
}
]
},
这是我的组成部分:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.getUser();
}
getUser() {
this.route.params.subscribe(params => {
console.log(params); // displays empty object
});
}
}
我做错了得到参数吗?
谢谢!
PS对我的代码的任何批评或建议都非常受欢迎!
答案 0 :(得分:2)
您正在加载与Child相同的Component,这是一种wiered
为此你的代码看起来应该是
{
path: 'user/:id',
component: UserComponent
}
或强>
{
path: 'users',
component: UserComponent, // For showing list of users
children: [
{
path: ':id',
component: UserDetailComponent // For Showing detail of specific user
}
]
},