1
到现在为止,我一直在做下面的订阅的值赋给我的班属性:分配值,而不突变
export class ExampleComponent implements OnInit {
exampleId: string;
constructor(public route: ActivatedRoute) {
this.route.params.subscribe((params: Params) => this.exampleId = params.id);
}
ngOnInit() {
// Do something with exampleId here...
}
}
在努力摆脱突变,更多地转向不变性;我希望将该订阅返回的值分配给exampleId
属性,而无需在构造函数中这样做。
什么是最好的方式去做这件事?
你可以在OnInit生命周期中做到这一点 –
这仍然会改变原来的属性定义。我需要在创建属性时分配值。 –
有一个快照替代获取路径参数,而不使用可观察的订阅[链接](https://angular.io/guide/router#snapshot-the-no-observable-alternative) –