首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么angular13中的数据不能正确地通过路由?

为什么angular13中的数据不能正确地通过路由?
EN

Stack Overflow用户
提问于 2022-03-10 15:50:42
回答 1查看 140关注 0票数 0

我有一个angular13应用程序,在这个应用程序中,我通过route data将数据从一个component传递到另一个component。在我的componentA中,我有一个函数

代码语言:javascript
复制
navigateTo(data: any){
  this._router.navigateByUrl(url, { state: data});  // {id: 1, age: 3} //
}

在我的componentB中,我以这种方式访问数据

代码语言:javascript
复制
  constructor(
   private router: Router,
   private route: ActivatedRoute,
   private location: Location
  ) {
       if (this.router.url.includes('edit')) {
          this.isEdit = true;
          console.log(this.location.getState());
       }
  }

但它总是用consolecomponentB中打印。

代码语言:javascript
复制
{navigationId:  6 //(random id each time)}

为什么数据不能正确传递?

EN

回答 1

Stack Overflow用户

发布于 2022-03-11 06:35:50

我没有理解您的问题,因为我猜"this.location.getState()“不是获取数据的正确方法(至少,以”角度“的方式)。

试试这个:

代码语言:javascript
复制
console.log(this.router.getCurrentNavigation().extras.state.id)

更多信息这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71427018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档