小编Oja*_*e S的帖子

对象可能为 Null

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

getStudent(): void {
  const id = + this.route.snapshot.paramMap.get('id');
  this.studentService.getStudent(id)
     .subscribe(student => this.SpecificStudent = student);
Run Code Online (Sandbox Code Playgroud)

我试图通过使用断言来确保它不为空

// !

const id = + this.route.snapshot.paramMap.get('id')!;
Run Code Online (Sandbox Code Playgroud)

如果我这样做,它不会显示错误,但 alert(id) 给出 0,这是错误的

typescript visual-studio-code angular

4
推荐指数
1
解决办法
2630
查看次数

标签 统计

angular ×1

typescript ×1

visual-studio-code ×1