通过Angular中的路由路径发送数据

Mot*_*ine 130 routing angular angular-router

无论如何使用router.navigate将数据作为参数发送?我的意思是,像这个例子,你可以看到路由有一个数据参数,但这样做它不起作用:

this.router.navigate(["heroes"], {some-data: "othrData"})
Run Code Online (Sandbox Code Playgroud)

因为某些数据不是有效参数.我怎样才能做到这一点?我不想用queryParams发送参数.

Deb*_*ahK 392

关于这个主题存在很多混淆,因为有很多不同的方法可以做到这一点.

以下是屏幕截图中使用的相应类型:

private route: ActivatedRoute
private router: Router
Run Code Online (Sandbox Code Playgroud)

1)所需的路由参数:

在此输入图像描述

2)路由可选参数:

在此输入图像描述

3)路由查询参数:

在此输入图像描述

4)您可以使用服务将数据从一个组件传递到另一个组件,而根本不使用路由参数.

有关示例,请参阅:https://blogs.msmvps.com/deborahk/build-a-simple-angular-service-to-share-data/

我这里有一个这样的人:https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p = preview

private route: ActivatedRoute
private router: Router
Run Code Online (Sandbox Code Playgroud)

  • 谢谢您的回答!2) 和 3) 有什么区别?因为两者最终都会在 URL 中添加“?parameter=”。你能给我举个例子 4) 吗?我不知道该怎么做。 (4认同)
  • 数据仅在执行的应用程序内“共享”。如果用户刷新页面,则会从服务器重新加载整个应用程序,因此应用程序中不会保留任何先前状态。就普通应用程序而言,当用户刷新它时,就像关闭并重新打开该应用程序一样。如果您需要在刷新后保留状态,则需要将其存储在某个位置,例如本地存储或服务器上。 (3认同)
  • 我还有一个关于路由的Pluralsight课程,涵盖所有这些:https://app.pluralsight.com/library/courses/angular-routing/table-of-contents如果您有兴趣,可以免费注册一周更多信息. (2认同)
  • 只是一个小提示:`route` 类型是`ActivatedRoute`,而不是`Router`。 (2认同)

Vég*_*ánd 85

Angular 7.2.0附带了一种新方法

https://angular.io/api/router/NavigationExtras#state

发送:

this.router.navigate(['action-selection'], { state: { example: 'bar' } });
Run Code Online (Sandbox Code Playgroud)

收到:

constructor(private router: Router) {
  console.log(this.router.getCurrentNavigation().extras.state.example); // should log out 'bar'
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到一些额外的信息:

https://github.com/angular/angular/pull/27198

上面的链接包含这个例子,它可能有用:https: //stackblitz.com/edit/angular-bupuzn

  • 鉴于Angular 7中的新功能,这是正确的答案。 (6认同)
  • 是的,这就是为什么您只能从构造函数中的 this.router.getCurrentNavigation() 获取数据的原因。如果你在其他地方需要它,比如在 ngOnInit() 中,你可以通过“history.state”访问数据。来自文档:“传递给任何导航的状态。当导航执行时,可以通过从 router.getCurrentNavigation() 返回的 extras 对象访问该值。导航完成后,该值将被写入到 history.state在激活此路由之前调用 .go 或 location.replaceState 方法。” (5认同)
  • 另外请确保您不要尝试在ngOnInit()中接收额外对象,例如this.router.getCurrentNavigation()。extras。 (4认同)
  • @KinleyChristian 查看这个答案,如果您还有其他问题,请告诉我!/sf/answers/3842395301/ (3认同)
  • 使用这种方法,我将如何处理用户的浏览器刷新?在这种情况下,导航附加功能中的数据似乎消失了,因此没有机会在刷新时再次使用它。 (2认同)

dev*_*ish 15

最新版本的angular(7.2 +)现在可以选择使用NavigationExtras传递其他信息。

家庭组件

import {
  Router,
  NavigationExtras
} from '@angular/router';
const navigationExtras: NavigationExtras = {
  state: {
    transd: 'TRANS001',
    workQueue: false,
    services: 10,
    code: '003'
  }
};
this.router.navigate(['newComponent'], navigationExtras);
Run Code Online (Sandbox Code Playgroud)

newComponent

test: string;
constructor(private router: Router) {
  const navigation = this.router.getCurrentNavigation();
  const state = navigation.extras.state as {
    transId: string,
    workQueue: boolean,
    services: number,
    code: string
  };
  this.test = "Transaction Key:" + state.transId + "<br /> Configured:" + state.workQueue + "<br /> Services:" + state.services + "<br /> Code: " + state.code;
}
Run Code Online (Sandbox Code Playgroud)

输出量

在此处输入图片说明

希望这会有所帮助!

  • 这已经很旧了,但对于未来的访问者来说——你_必须_在构造函数中调用它。/sf/answers/3842395301/ (5认同)
  • 我不断收到“导航为空”的信息。我尝试过几个例子... (4认同)

小智 6

你可以用它.Angular2 +/4/5 - 使用数据导航 https://github.com/Hipparch/Angular2-navigate-with-data

  • 这看起来很丑陋 (4认同)

Ali*_*F50 6

我需要访问其中的数据,但由于循环依赖,CustomRouteReuseStrategy我无法在那里注入数据,但您也可以使用该对象来读取状态。RouterLocation

Send:

    this.router.navigate(['action-selection'], { state: { example: 'bar' } });

Receive:
    import { Location } from '@angular/common';

    constructor(private location: Location) {
      console.log(this.location.getState().example); // should log out 'bar'
    }
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

148183 次

最近记录:

6 年,6 月 前