需要将参数作为 query/path 传递,但在 URL 上不可见

Need to pass param as query/path but should not visible on URL

我需要将一个参数从一个路由传递到另一个路由,但它不应该对用户可见。我需要它 Angular 6.

如果您不想 URL 发生变化,您可以使用 skipLocationChange 策略。

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

你可以使用 .

然后使用 Angular location, and router 服务读取并覆盖 URL 历史记录。

它不优雅但有效..并实现了您的要求。

位置历史检查和重写示例:

this.router.events.pipe(filter(event => event instanceof NavigationEnd))
.subscribe((event: NavigationEnd) => {
  const matrixParams = this.location.path().split(';');

  ... do something with the matrixParam

  if(matrixParams.length > 1) {
    this.location.replaceState(matrixParams[0])
  }

}));

其他想法(如前所述):

  • 使用服务传递数据,在 OnInit 中检查它
  • 如前所述,使用@Input()在组件之间传递它