-1
export class UserComponent implements OnInit {
user: {id: number, name: string};
constructor(private route: ActivatedRoute) { }
ngOnInit() {
// this.user = {
// id: this.route.snapshot.params.id,
// name: this.route.snapshot.params.name
// }
this.user.id = this.route.snapshot.params.id;
this.user.name = this.route.snapshot.params.name;
}
}
ここにコードがあります。私はいくつかのデータをユーザーオブジェクトに割り当てようとしています。そして、方法-1でデータを割り当てようとしました。角2+有効なルートパラメータからデータを割り当てます
// method-1
this.user.id = this.route.snapshot.params.id;
this.user.name = this.route.snapshot.params.name;
実際には機能しませんでした。しかし、私はもう1つの方法-2を試みましたが、それは方法-1と非常によく似ていて、うまくいきました。私はこれら2つの方法の違いは何かと思います。
// method-2
this.user = {
id: this.route.snapshot.params.id,
name: this.route.snapshot.params.name
}