【问题标题】:I am not able to extract Query params from Url in Angular 6我无法从 Angular 6 中的 Url 中提取查询参数
【发布时间】:2019-12-26 22:24:16
【问题描述】:

我将 Id 作为参数传递给我 mu 路由路径,但是当我提取婴儿车时出现错误。

我尝试使用 route.params.subscribe() 方法抽象参数。

我以键名作为术语传递 ID 的方法

onEdit(id:string) {
    console.log(id);   
    this.router.navigate(['../UpdateOrganization',{ term: id }], { relativeTo: this.route }); 
  }

我的网址如下所示:-

http://localhost:4200/#/dashboard/UpdateOrganization;term=MSI3563

我从 url 中提取查询参数的方法

ngOnInit() {
    this.createFormControls();
    this.createForm();
    this.route.params.subscribe(params => {
      if (params['term']) {
        this.selectedId = params['term'];
      }
    });
    console.log(this.selectedId);
  }

我在控制台中收到以下错误消息

ERROR TypeError: Cannot read property 'params' of undefined

【问题讨论】:

    标签: angular routing parameter-passing angular2-routing url-routing


    【解决方案1】:

    查询参数通常有一个?在他们之前的网址 所以http://localhost:4200/#/dashboard/UpdateOrganization;term=MSI3563 实际上应该是http://localhost:4200/#/dashboard/UpdateOrganization?term=MSI3563

    我认为您从那里缺少 queryParams 对象 this.router.navigate(['../UpdateOrganization', { queryParams: { term: id }}], { relativeTo: this.route });

    要阅读查询参数,您必须订阅 queryParams 而不是参数

      this.route.queryParams.subscribe(params => {
            this.param1 = params['term'];
        });
    

    在构造函数中将路由设置为激活路由

    constructor(private route: ActivatedRoute){}

    【讨论】:

    • 同样的问题还在继续
    • 可以分享一下你的构造函数吗,路由的类型是什么?
    • 我已经编辑了我的答案以显示构造函数的外观
    • 构造函数(私有getOrgList:GetOrganizationListService,私有路由器:路由器,私有路由:ActivatedRoute){}
    • 我和你的一样
    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 2018-11-10
    • 2018-05-07
    • 2016-06-11
    • 2020-12-10
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    相关资源
    最近更新 更多