【问题标题】:Angular2: How to access index.html querystring in App ComponentAngular2:如何在 App 组件中访问 index.html 查询字符串
【发布时间】:2017-04-06 13:53:03
【问题描述】:

我是 Angular2 开发的新手。

我有一个要求,我需要从我的 ASP.Net 网站调用 Angular2 网站。在调用 Angular2 网站 (localhost:4200\index.html) 时,将传递如下查询字符串 localhost:4200\index.html?UserID=Chandra

现在我的问题是。

如何获取 UserId 查询字符串值到 App 组件(即 BootStrap 组件)或任何其他组件

请帮我解决这个问题。

提前致谢。

【问题讨论】:

    标签: angular


    【解决方案1】:

    docs 在这方面相当不错,如果你没有看过的话。简短的回答是,要从 a 组件访问查询参数,您需要使用 ActivatedRoute

    import { ActivatedRoute } from '@angular/router';
    
    ....
    
    export class MyComponent {
    
        constructor(
          private route: ActivatedRoute,
        ) { }
    
       ngOnInit() {
         this.route.queryParams.subscribe(queryParams =>
           //do something with queryParams['UserID']
         );
       }
    
    }
    

    路由器将同时保存路由参数和查询参数。它们被分开处理。 SubscribequeryParams 以访问这些参数。

    希望对您有所帮助。

    【讨论】:

    • 非常感谢。它对我有用。但是没有 语句,它的抛出错误。
    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多