【问题标题】:How to access query params from a url using activated router in angular?如何使用角度激活的路由器从 url 访问查询参数?
【发布时间】:2020-10-06 11:59:04
【问题描述】:

我需要使用激活路由器通过查询参数访问当前 url。

 constructor(private router: Router,
              private route: ActivatedRoute) {
  }

 ngOnInit() {
    this.router.events.pipe(
          filter((event: RouterEvent) => event instanceof NavigationEnd && event.url.startsWith('/resources'))
        ).subscribe(() => {
          console.log(this.route.snapshot.params)
        })

}

当我访问我的网址时,http://localhost:4200/web/#/resources/sharepoint;siteId=docs;displayName=test;resourceId=4

this.route.snapshot.params

打印一个空数组。如何访问查询参数“resourceId”?

我尝试了this.route.snapshot.queryParams,但它仍然打印一个空数组。

【问题讨论】:

    标签: angular angular-router angular-activatedroute


    【解决方案1】:

    this.router.url阅读然后拆分它

    constructor(private router: Router) {
       var url=this.router.url;
        var params=url.split(";");
        var resourceId=params[params.length-1].split("=")[1];
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-15
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多