【问题标题】:Need to pass param as query/path but should not visible on URL需要将参数作为查询/路径传递,但不应在 URL 上可见
【发布时间】:2018-07-11 07:03:46
【问题描述】:

我需要将参数从一条路线传递到另一条路线,但它不应该对用户可见。我在 Angular 6 中需要它。

【问题讨论】:

  • 如果它不应该在 URL 中,则不能将其作为查询或路径参数传递。
  • 您可以使用@Input 装饰器并通过该变量传递值。
  • 您可以在服务中保持价值。
  • 我有来自响应的变量,需要将它传递给另一个 ts 组件到它路由到下一步的位置,同时我不想在 url 上查看该变量。

标签: angular typescript angular6


【解决方案1】:

你可以使用matrix params

然后使用 Angular locationrouter 服务读取并覆盖 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() 在组件之间传递它

【讨论】:

    【解决方案2】:

    如果您不想更改 URL,可以使用 skipLocationChange 策略。

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

    【讨论】:

      猜你喜欢
      • 2010-12-25
      • 1970-01-01
      • 2021-11-15
      • 2013-01-25
      • 2020-07-27
      • 1970-01-01
      • 2014-06-14
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多