1 回答

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊
在您的路由器鏈接中將代碼更改為此,尾隨的“/”無法與您定義的路由匹配:
[routerLink]="['/edit', emp.employeeId]"
同樣在您的組件中,您必須使用"id"以下方式獲取參數(shù):
this._avRoute.snapshot.params['id']
但更好的方法是在ngOnInit()生命周期鉤子中調(diào)用服務(wù),而不是constructor. 您需要訂閱 Observable ActivatedRoute.params,它將對(duì) URL 中參數(shù)的更改做出反應(yīng)。
@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']);
});
}
}
我為你創(chuàng)建了一個(gè)小演示:https : //stackblitz.com/edit/angular-hdvpzm
- 1 回答
- 0 關(guān)注
- 156 瀏覽
添加回答
舉報(bào)