1 回答

TA贡献1834条经验 获得超8个赞
在您的路由器链接中将代码更改为此,尾随的“/”无法与您定义的路由匹配:
[routerLink]="['/edit', emp.employeeId]"
同样在您的组件中,您必须使用"id"以下方式获取参数:
this._avRoute.snapshot.params['id']
但更好的方法是在ngOnInit()生命周期钩子中调用服务,而不是constructor. 您需要订阅 Observable ActivatedRoute.params,它将对 URL 中参数的更改做出反应。
@Component({
selector: 'hello',
template: `<h1>Employee Details</h1>
<div *ngIf="emps$ | async; let emps">
<h3> Id: {{emps.id}}</h3>
<h3> Name: {{emps.name}}</h3>
<h3> Age: {{emps.age}}</h3>
</div>`,
styles: [`h1, h3 { font-family: Lato; }`]
})
export class EditEmployeeComponent {
emps$: Observable<any>;
constructor(private _empService: EmployeeService, private _avRoute: ActivatedRoute, private _fb: FormBuilder){
}
ngOnInit(){
this._avRoute.params.subscribe(params => {
this.emps$ = this._empService.getEmployeeDetails(params['id']);
});
}
}
我为你创建了一个小演示:https : //stackblitz.com/edit/angular-hdvpzm
- 1 回答
- 0 关注
- 160 浏览
添加回答
举报