angular - route.queryParams.subscribe 在 Angular Typescript 组件中返回未定义

标签 angular typescript url-routing angular2-routing

我需要将 id 作为参数传递给我的 Angular 组件

在我的html中我有这样的东西

<a [routerLink]="['/viewjobdetail',  currentJob.id]">
    {{ currentJob.title }}
</a>

app.module.ts RouterModule.forRoot 配置为

  { path: 'viewjobdetail/:id', component: JobDetailComponent }

在我的调用组件中有

import { ActivatedRoute } from '@angular/router';
constructor(private route: ActivatedRoute){}

ngOnInit(): void {
this.route.queryParams.subscribe(params => {
  debugger;
  var a = params['id'];
  this.displayJobId = parseInt(params['id']);
});

但是当我检查它总是显示undefined时,我在这里做错了什么?

最佳答案

您应该使用paramsparamMap 而不是queryParams,查询参数只能识别像viewjobdetail?id= 这样传递的查询参数23

queryParams 更好,因为 params 可能在未来的版本中被弃用

this.route.paramMap.subscribe(paramMap => {
  debugger;
  var a = paramMap.get('id');
  this.displayJobId = parseInt(a);
});

关于angular - route.queryParams.subscribe 在 Angular Typescript 组件中返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65640399/

相关文章:

html - angular 2句柄点击iframe内的链接

html - Angular 8中根据是否有overflow-x(水平滚动)显示和隐藏元素

typescript 我无法通过 "import"命令导入 choice.js

wordpress - 在 WordPress PHP 中将长域 URL 重写为短域 URL

node.js - 以 Angular 创建新项目时出错

Angular(5)-Electron 串口支持

javascript - AngularJS 路由配置

asp.net - Web.config : Wildcards in location and authorization

javascript - Angular 6 setTimeout 和 clearTimeout 错误

javascript - 删除递归对象条目