我想从 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,这是错误的