gpt4 book ai didi

angular - 对象可能为 Null

转载 作者:行者123 更新时间:2023-12-04 11:51:45 24 4
gpt4 key购买 nike

我想从 url 中获取 id,然后使用它导航到不同的特定 View
我有以下功能

getStudent(): void {
const id = + this.route.snapshot.paramMap.get('id');
this.studentService.getStudent(id)
.subscribe(student => this.SpecificStudent = student);
我试图通过使用断言来确保它不为空
//!
const id = + this.route.snapshot.paramMap.get('id')!;
如果我这样做,它不会显示错误,但 alert(id) 给出 0,这是错误的

最佳答案

您可以使用 Number方法:

Number(this.route.snapshot.paramMap.get('id'))
或者
this.route.paramMap.subscribe(param => {
let id = +param.get('id');
})

关于angular - 对象可能为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65636931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com