简体   繁体   English

查询 - 参数 <app-route> 聚合物元素

[英]query-params in <app-route> element of polymer

I am using the <app-route> element in my application. 我在我的应用程序中使用<app-route>元素。 I want to capture the queryParams in the URL 我想在URL中捕获queryParams

eg 例如

localhost:8080/#/test?foo=bar&baz=qux 本地主机:8080 /#/测试富=栏&巴兹= qux

The code I have written: 我写的代码:

<app-location id="location" route="{{route}}" use-hash-as-path></app-location>
<app-route id="route"
        route="{{route}}"
        pattern="/:page"
        data="{{routeData}}"
        tail="{{subroute}}"></app-route>

when I use the above URL I get the 当我使用上面的URL我得到了

this.$.route.routeData.page = "test?foo=bar&baz=qux"
this.$.route.queryParams = {}

I was expecting 我在期待

this.$.route.routeData.page = "test"

and

this.$.route.queryParams = {
   foo : "bar",
   baz : "qux"
}

I looked at the example in polymer documentation, but it did not help. 我查看了聚合物文档中的示例,但它没有帮助。 What am I missing? 我错过了什么?
Am I expecting something wrong? 我有点不对劲吗? How is queryParams supposed to work? queryParams应该如何工作?

Any help would be appreciated. 任何帮助,将不胜感激。

Bind to <app-location>.queryParams to get the query parameters: 绑定到<app-location>.queryParams以获取查询参数:

<app-location route="{{route}}" query-params="{{queryParams}}">

Note also that <app-location>.route has an internal property that contains the query parameters: __queryParams . 另请注意, <app-location>.route具有包含查询参数的内部属性: __queryParams Technically, you could use that with the caveat of it possibly being unusable in the next release. 从技术上讲,您可以使用它,但在下一个版本中它可能无法使用。 (ie, this.route.__queryParams === this.queryParams ). (即this.route.__queryParams === this.queryParams )。

codepen codepen

The reason you get the answer you do is that the url is wrong. 你得到答案的原因是网址错误。 The # part comes at the end. #部分结束了。

Try url 试试网址

localhost:8080/?foo=bar&baz=qux#/test 本地主机:8080 /富=栏&巴兹= qux#/测试

The clue is in the page parameter where it appears that the query params have been added to the page parameter. 线索在页面参数中,看起来查询参数已添加到页面参数中。 They haven't, but the browser just thinks they are part of the hash and passes them over in window.location.hash . 他们没有,但浏览器只是认为它们是哈希的一部分并在window.location.hash传递它们。 app-location parsing of the hash part splits on '/' so thinks the whole thing is part of the :page pattern. 散列部分的app-location解析在'/'上拆分,因此认为整个事情是:页面模式的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM