【问题标题】:Get URL parameters in Angular在 Angular 中获取 URL 参数
【发布时间】:2019-11-11 14:59:05
【问题描述】:

我正在尝试以角度获取 url 参数 我试过了

 ngOnInit() {   

   this.route.paramMap.subscribe(params => {
     console.log("test1",params)
}) 
//and
console.log("test2", this.route.snapshot.params)

 }
ngAfterViewInit(){


}

它得到的响应: enter image description here

【问题讨论】:

标签: angular routes


【解决方案1】:
constructor() {
    const query = new URLSearchParams(location.search);
    console.log(query.get('param_key')); // www.google.com?size=123 --- param_key is size here

}

试试这个,如果遗漏什么,请告诉我......快乐编码...... :)

【讨论】:

  • @SoufianChaoui 你使用了错误的密钥,看看这个... this.route.queryParams.subscribe( params => { console.log(params[param_key]); } )
【解决方案2】:

您使用的是ActivatedRouter 类还是Router 类? 当路由器属于路由器类时,这应该可以工作。

import { Router } from '@angular/router';

...
constructor(private router: Router) {
     this.router.params.subscribe(params => {
     console.log('my parameters: ', params);
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2018-05-29
    • 2014-01-06
    • 2018-02-10
    • 1970-01-01
    • 2016-02-07
    • 2014-05-17
    相关资源
    最近更新 更多