简体   繁体   English

路由中的Polymer和Page.js通配符

[英]Polymer and page.js wildcards in routes

Am using the polymer starting kit and am trying to access routes to users that does not necessary have a link to it on the page. 我正在使用聚合物入门工具包,并试图访问到用户的路线,该路线在页面上没有必要链接。

routing.html: routing.html:

page('/users/:name', function(data) {
  app.route = 'user-info';
  app.params = data.params;
});

On the page i have: 在页面上,我有:

<a href$="{{baseUrl}}users/Addy">Addy</a><br>
<a href$="{{baseUrl}}users/Rob">Rob</a><br>
<a href$="{{baseUrl}}users/Chuck">Chuck</a><br>
<a href$="{{baseUrl}}users/Sam">Sam</a>

It is matching the 4 users with the specified urls correct, but i want the route to match any name even tho there is no link to the page. 它正确匹配了具有指定url的4个用户,但是我希望路由匹配任何名称,即使没有指向页面的链接也是如此。

It will work with the route '/users/*' that will match everything, but i do not want to use that if i dont have to. 它将与将匹配所有内容的路由“ / users / *”一起使用,但是如果我不需要的话,我不想使用它。

That route you have there will achieve what you want. 您在那里的那条路线将实现您想要的。 The way page.js uses the : character is as an identifier that what follows after this should be treated as a parameter. page.js使用:字符的方式是作为标识符,之后应将其视为参数。 Lets say we have the following route: 假设我们有以下路线:

page('/users/:name', function(data) {
 app.route = 'user-info';
 app.params = data.params;
});

With that we can match the following urls: 这样我们可以匹配以下URL:

/users/Addy
/users/Chuck
/users/somebody

The :name part of the route can be whatever we want and it will be matched. 路由的:name部分可以是我们想要的任何内容,它将被匹配。 To know what the :name part is on your polymer code you need to get it from the function that we specified when we defined the route. 要知道您的聚合物代码中的:name部分是什么,您需要从定义路线时指定的函数中获取它。 The data parameter that is passed on the function contains that info. 在函数上传递的data参数包含该信息。

On the route we have defined here, we can access the :name parameter with data.params.name inside the route definition. 在此处定义的路由上,我们可以使用路由定义内的data.params.name访问:name参数。

page('/users/:name', function( data ){
  //We have access to the :name parameter here
});

Best way to let the elements that want the data to know what it is. 让需要数据的元素知道数据是什么的最佳方法。 Is to set your :name parameter to a variable on your app object. 是将:name参数设置为app对象上的变量。

page('/users/:name', function( data ){
  app.route = 'user-info';
  app.params = data.params;
});

That code sets the whole parameter data to a variable on your app object. 该代码将整个参数数据设置为您的app对象上的变量。 In this case we want to access the :name parameter. 在这种情况下,我们要访问:name参数。 The way we go about it is to pass the app.params object to the element that needs that data and access the :name parameter with params.name , that assumes that you have named the element property in whic you use it params . 我们要解决的方法是将app.params对象传递给需要该数据的元素,并使用params.name访问:name参数,假设您在使用params已经命名了element属性。

<custom-elem params="{{params}}></custom-elem>

Expecting that element to be child element of the <template is="dom-bind" id="app"> element. 期望该元素成为<template is="dom-bind" id="app">子元素<template is="dom-bind" id="app">元素。

With that in mind we can go to any route that matches the route so /users/anything and if you have set up your user page properly you will see the info for the user in this case info for anything user. 考虑到这一点,我们可以去那这样的路径匹配任何路径/users/anything ,如果你已经设置了你的用户页面正常,你将看到在这种情况下,信息用户的信息anything用户。

You can read more about routing with page.js here: Visionmedia Page.js 您可以在此处阅读有关使用page.js进行路由的更多信息: Visionmedia Page.js


To solve the issue from coming from outside the current site use following code. 要解决来自当前站点之外的问题,请使用以下代码。

var users = function(data){
  app.route = 'user-info';
  app.params = data.params;
};
page('/users/:name', users);
page('//users/:name', users);

It isn't the most elegant thing but will solve the issue. 这不是最优雅的事情,但可以解决问题。 It will cause ugly urls that have two slashes. 它将导致带有两个斜杠的丑陋网址。


Better way to solve the second problem. 解决第二个问题的更好方法。 Don't us the hashbangs: true option set it false . 不要让我们成为hashbangs: true选项将其设置为false Instead set the base url to this: page.base('/#'); 而是将基本URL设置为此: page.base('/#'); That solves the problem and removes the annoiyng ! 这样就解决了问题,消除了烦恼! from the url. 从网址。 :) :)

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

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