【问题标题】:How to get all params after the root path如何在根路径之后获取所有参数
【发布时间】:2019-12-22 15:01:18
【问题描述】:

我的应用在root 下运行。我想提取root之后的任何文本。

在下面的 URL 中,我想提取 /a1/b1

http://localhost:4200/root/a1/b1

下面是我的代码,

this.route.params.pipe(takeUntil(this.unsubscribe$)).subscribe(params => {
  this.urlText = params.ID;
});

这只是给我/a1

【问题讨论】:

  • 你可以试试这样的:this.urlText = this.route.url.split("root")[1];
  • 我收到此错误Property 'split' does not exist on type 'Observable<UrlSegment[]>'.ts(2339)
  • 抱歉,我的意思是this.router,其中router 的类型是Router
  • 这似乎工作正常

标签: javascript angular typescript angular2-routing


【解决方案1】:

我假设您已经在角度路由文件中声明了参数的名称。所以你可以从 @angular/router 导入路由并使用 this.route.snapshot.params["paramName"]。

例如 如果你的路线是

'localhost4200/:root/:a1/:b1'

在您的组件文件中导入路由并在构造函数中声明它,如私有路由:路由器和在 ngOnInit 中使用

this.route.snapshot.params["a1"]
this.route.snapshot.params["b1"]

这将完成工作

【讨论】:

  • 它可以是 /a1/b1/c1 等等...每次我不想重复这些行
  • 我想得到合并的参数
猜你喜欢
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
  • 2014-10-16
  • 2019-01-24
  • 1970-01-01
  • 2018-11-13
相关资源
最近更新 更多