Angular 5 从组件读取参数
Angular 5 read parameter from component
我有一个 table 组件列出了带有 link 的用户以重定向到另一个组件以编辑记录。从 table 组件我调用详细信息组件,我想在其中调用服务来获取我的用户记录。虽然我正确地传递了 id 参数,但我不确定从详细信息组件中读取它的正确方法是什么。
table分量:
<tbody>
<tr *ngFor="let user of users">
<td><a routerLink="/users/details/{{user.id}}">{{user.name}}</a></td>
<td>{{user.Name}}</td>
</tr>
</tbody>
在 AppModule 中注册路由器:
{ path: 'users/details/:id', component: DisplayUserComponent }
下面是我计划从详细信息组件调用服务的方式:
export class DisplayUserComponent {
constructor(private userService: UserService) { }
ngOnInit() {
this.userService.getUserById(???????USER_ID????????)
.subscribe(res => console.log(res));
}
}
同样,我的问题是如何从通过路由传递的组件中获取用户 ID。欢迎任何帮助!
有很多方法可以读取url参数。看看
虽然,我会和@DanielWStrimpel 一起去,我认为你只需要 this.
我有一个 table 组件列出了带有 link 的用户以重定向到另一个组件以编辑记录。从 table 组件我调用详细信息组件,我想在其中调用服务来获取我的用户记录。虽然我正确地传递了 id 参数,但我不确定从详细信息组件中读取它的正确方法是什么。
table分量:
<tbody>
<tr *ngFor="let user of users">
<td><a routerLink="/users/details/{{user.id}}">{{user.name}}</a></td>
<td>{{user.Name}}</td>
</tr>
</tbody>
在 AppModule 中注册路由器:
{ path: 'users/details/:id', component: DisplayUserComponent }
下面是我计划从详细信息组件调用服务的方式:
export class DisplayUserComponent {
constructor(private userService: UserService) { }
ngOnInit() {
this.userService.getUserById(???????USER_ID????????)
.subscribe(res => console.log(res));
}
}
同样,我的问题是如何从通过路由传递的组件中获取用户 ID。欢迎任何帮助!
有很多方法可以读取url参数。看看
虽然,我会和@DanielWStrimpel 一起去,我认为你只需要 this.