javascript - 访问 Angular 6 中的动态 URL 参数

标签 javascript node.js angular typescript ecmascript-6

在我的 angular 7 代码中,我尝试获取 URL 中传递的 clientIdclientId 将是动态的。

localhost:4200/client/xyz

app.routing.ts

 {
  path: 'client/:clientId',
  component: AppComponent
 },

app.component.ts

constructor(private route: ActivatedRoute) { }

ngOnInit() {
   console.log(this.route.snapshot.paramMap.get('clientId'));
});

它在控制台中打印 null

最佳答案

您可以使用此方法获取路由参数

this.route.params.subsribe(params => {
  console.log(params['clientId'])
});

this.activateRoute.snapshot.params['clientId']

如果您仍有问题,请告诉我

关于javascript - 访问 Angular 6 中的动态 URL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56258671/

相关文章:

javascript - 光滑 slider 的响应问题?

javascript - 分割和映射后输出错误

php - 高端 Node.js 框架

javascript - 履行后将 promise 数组转换为值数组

javascript - 错误: [$injector:nomod] when attempting to add 3rd party module to mean app.

javascript - ES6 : import many files

javascript - Angular : Directive transcluded scope lost

angular - 子路线最初未以 Angular 加载

angular - 如何在 Ionic 中声明 WindowsAzure?

angular - 我们如何在 Angular 服务中使用forwardRef来避免循环依赖?