【问题标题】:passing id in (click) to another component or route将 id 传入(点击)到另一个组件或路由
【发布时间】:2020-11-27 19:17:40
【问题描述】:

我需要将一个 id 与一个点击事件一起传递给另一个组件

这里是组件.ts

 editData(rcid){
      this.router.navigate(['/Editdata']);
  }

rcid 是被点击项目的 id。而router“/editdata”是传递id的地方。

我已经检查了会话和本地存储,但是当它带有另一个 id 时需要刷新页面

请帮我解决这个问题。我是角度新手

【问题讨论】:

  • 为什么不在路由中传递呢?使用查询或路径参数。
  • @AakashGarg 我不知道你能不能举个例子
  • @AakashGarg id 已传递给组件,但我在服务组件中需要它。有办法吗?
  • 可以是组件也可以是服务,什么是服务组件?

标签: angular angular-routing angular-component-router


【解决方案1】:

您可以将第二个参数传递给实现NavigationExtras interfacenavigate

this.router.navigate(['/Editdata'], { state: { id: '123' } });

在您的接收组件中,您可以提取它:

constructor(private router: Router) {
  this.id = this.router.getCurrentNavigation().extras.state.id;
}

【讨论】:

  • 它向我显示了这个错误Error: Cannot instantiate cyclic dependency!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 2018-01-06
相关资源
最近更新 更多