【问题标题】:Angular Routing with "?" before hash带有“?”的角度路由哈希之前
【发布时间】:2017-11-10 17:01:34
【问题描述】:

我们需要在url前面插入一个带问号的hash。就像

localhost:4200/?哈希码

并且只读取哈希值。

现在是在不带问号的情况下读取它,但是当我们将它放在哈希之前,它不起作用。

我们如何正确配置去路由来达到这个要求?

提前致谢,

【问题讨论】:

  • 你的意思是'localhost:4200/?key=value'?
  • 我的意思是 localhost:4200/?
  • 你为什么不使用@ArmenVardanyan 建议的查询参数。它在 Angular 中原生支持。
  • 所以您只想将参数传递给您的路线,对吧?
  • 这不是我的决定,出于某种原因,这是业务需求。有可能吗?

标签: angular angular4-router


【解决方案1】:

你试图完成的事情是不可能和不正确的。查询参数首先是使用 URL 传输数据的标准的一部分,这些参数是 key-value。在 Angular 中,您可以执行以下操作:

{path: ':hashcode', component: MyComponent} // your route
// in MyComponent
constructor(private route: ActivatedRoute){}
ngOnInit() {this.route.params.subscribe(params => // do something with params)}

或者这样:

{path: '', component: MyComponent // you don't need to define query parameters inside the route definition}
// same code inside the component, but just subscribe to queryParams Observable instead of params 

这个“业务需求”很奇怪,没有必要而且没用。你需要面对它。

【讨论】:

    猜你喜欢
    • 2017-03-25
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2020-05-21
    相关资源
    最近更新 更多