使用路由器传递数据 [英] passing data using router

查看:90
本文介绍了使用路由器传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将数据传递到其他组件时遇到问题.这是示例.

I have a problem with passing data to other component. Here is the example.

 onEdit(data) {
    this.router.navigate(["/edit", { state: data }], {
      relativeTo: this.activatedRoute
    });
  }

还有我的路由器ts文件:

{
    path: "edit",
    component: ServiceTimeGoalUpdateComponent,
    resolve: {
      serviceTimeGoal: ServiceTimeGoalResolve
    },
    data: {
      pageTitle: "ServiceTimeGoals",
      data: "data"
    }
  },

上面的摘录给了我:

And above snippets gives me:

我真正想要的是,当我单击列表中的编辑"按钮时,它应该导航到edit并包含一些数据.我在做什么错了?

What i exactly want is when i click to edit button from the list it should be navigate to edit and with some data. What am i doing wrong ?

推荐答案

@batgerel,您的代码中有错误.必须

@batgerel, You has an error in your code. must be

this.router.navigate(['./edit'], 
   { relativeTo: this.activatedRoute, state: { data } })

并且,如果您的组件不是主应用程序,请使用ngOnInit

And, if your component it's not the main app, in ngOnInit

   this.activatedRoute.paramMap.pipe(
                take(1), //take(1) is for unsubscribe
                map(() => window.history.state)
            ).subscribe(res => {
                console.log(res.data)
            })

这篇关于使用路由器传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆