Oja*_*e S 4 typescript visual-studio-code angular
我想从 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,这是错误的
您可以使用以下Number
方法:
Number(this.route.snapshot.paramMap.get('id'))
Run Code Online (Sandbox Code Playgroud)
或者
this.route.paramMap.subscribe(param => {
let id = +param.get('id');
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2630 次 |
最近记录: |