获取 ionic 5 中 url 传递的参数

Get parameters passed by url in ionic 5

我已将 URL 中的参数传递给我的离子应用程序

http://localhost:8100/welcompage/overview?brand=bmw

我正在使用 ActivatedRoute 检索在 URL

中作为参数传递的数据
   constructor(private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.route.params.subscribe(params => {
             console.log(params['brand']);
        });
    }

输出总是“未定义”同时URL

中有参数

第一种方法:

  let brand = this.route.snapshot.paramMap.get('brand');

第二种方法:

this.route.paramMap.subscribe(

  (data) => {

    console.log(data.brand)

  }

);

当您已经在路线上定义了品牌参数时,方法 1 或方法 2 可以满足您的需求 url/:brand。 但是如果你使用查询参数 url?brand=value1&property2=vaule2... 您可以使用 method3:

获取查询参数数据

方法三:

this.route.queryParams
  .subscribe(params => {
    console.log(params); // { brand: "bmw" }
    let brand = params.brand;
    console.log(brand); // bmw
  }
);