ActivatedRoute.queryParams未定义

Isa*_*vin 5 routing angular

我正在尝试使用Angular 2.0捕获查询字符串参数,并且我很难过.我有一个这样的网址

http:// localhost:5000 /?id_token = eyJ0eXAiO ....

我希望能够在路由占用之前捕获id_token的值,并且我丢失了参数.我发现了这个GitHub问题线程

https://github.com/angular/angular/issues/9451

但是这使用了一种不再适用的弃用方法.我尝试了以下方法

    var foo = this.activatedRoute.queryParams.subscribe(params => {
        console.log(params);
        let id = params['id_token'];
        console.log(id);
    });


   console.log(this.router.routerState.snapshot.root.queryParams["id_token"]);

   var bar = this.activatedRoute.params.subscribe(
        (param: any) => console.log(param['id_token']));
Run Code Online (Sandbox Code Playgroud)

我在构造函数中尝试了这些方法以及我的组件的ngOnInit(),但控制台始终显示未定义.我是在错误的地方做这件事吗 我根本无法更改QS,因为它是由Azure AD创建的.

Sim*_*Zyx 5

尝试注入 ActivatedRoute 并使用route.snapshot.queryParams['name']如下方式获取 queryParam :

class SomeComponent implements OnInit {
    constructor(private route: ActivatedRoute) { }
    ngOnInit(): void {
        let value = this.route.snapshot.queryParams['name'];
    }
}
Run Code Online (Sandbox Code Playgroud)

通过 router.navigate 设置 queryParams 的工作方式如下:

class SomeComponent {
    constructor(private router: Router) { }
    goSomewhere(): void {
        this.router.navigate(['somewhere'], { queryParams: { name: 'value' } });
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @JonTinsman 你解决了吗?我正面临着确切的问题 (2认同)