简体   繁体   English

Polymer core-animated-pages默认页面

[英]Polymer core-animated-pages default page

I'm using the Polymer core-animated-pages to switch between my websites main pages. 我正在使用Polymer core-animated-pages在我的网站主页之间切换。

I select which page needs to be shown by the id of that <section> . 我选择哪个页面需要通过<section>的id显示。 You can see a sample in action here . 您可以在此处查看示例。 Now, the issue I'm having is that at loading of the page, the page that should be selected gets loaded from the url, eg www.example.com/home shows the home page, www.example.com/activities shows the activities page (code left out of example since not really relevant). 现在,我遇到的问题是,在加载页面时,应该从网址加载应该选择的页面,例如www.example.com/home显示主页, www.example.com/activities显示活动页面(代码遗留的例子,因为不相关)。

But what should I do when the id provided in the link doesn't exist? 但是,如果链接中提供的ID不存在,我该怎么办? Is there an option to show a default core-animated-pages -page with a 404 message? 是否有选项显示带有404消息的默认core-animated-pages -page? Or do I have to check every link if it's in an array of all my pages, if so load the error page manually and else show the correct page? 或者我是否必须检查每个链接是否在我的所有页面的数组中,如果是这样,手动加载错误页面,否则显示正确的页面?

Again, here's the example: jsbin . 再次,这是一个例子: jsbin

Edit: To show the way my page handles linking, here's an update example: jsbin . 编辑:要显示我的页面处理链接的方式,这是一个更新示例: jsbin Linking is essentially www.example.com/#home , etcetera 链接本质上是www.example.com/#home

Something I'm missing from reviewing your example is how you're handling routing. 在审查您的示例时,我缺少的是您如何处理路由。 In a typical site, if a user navigates to example.com/foo or example.com/#foo my expectation would be (using your current setup) that that would take you to the corresponding core-animated-pages page with the ID foo or a section which corresponds to that route. 在典型的站点中,如果用户导航到example.com/fooexample.com/#foo我的期望(使用您当前的设置)将带您到ID为foo的相应core-animated-pages页面或与该路线相对应的部分。

Using routing (maybe the Polymer flatiron-director element), a basic solution might query the DOM in your element to see if a section of ID notsupportedURL can be found. 使用路由(可能是Polymer flatiron-director元素),基本解决方案可能会查询元素中的DOM,以查看是否可以找到ID notsupportedURL的一部分。 If not, default to taking the user to your core-animated-pages 404 page, which doesn't have to be any more complex than what you have in place now. 如果没有,默认情况下将用户带到您的core-animated-pages 404页面,该页面不必比现在更复杂。

There are a couple of things you could do here. 你可以在这里做几件事。

In the simplest case, you could just validate the value in the pageChanged handler. 在最简单的情况下,您可以只在pageChanged处理程序中验证值。 If the page doesn't exist, update it: 如果该页面不存在,请更新它:

pageChanged: function(){ var foundPage = this.$.pages.querySelector('#' + this.page); if (! foundPage) { this.page = 'error' } ...

See: http://jsbin.com/xequvone/14/edit 请参阅: http//jsbin.com/xequvone/14/edit

If you don't bind the core-menu directly to the core-animated-pages , you can add whatever logic you want. 如果不将core-menu直接绑定到core-animated-pages ,则可以添加所需的逻辑。 The following version does exactly the same thing as the previous one, but uses a separate variable for the selected page: 以下版本与前一版本完全相同,但对所选页面使用单独的变量:

http://jsbin.com/xequvone/12/edit http://jsbin.com/xequvone/12/edit

I think you'd want a router if your URL scheme was more complex. 如果你的URL方案更复杂,我想你想要一个路由器。 For example, if the hash includes multiple levels and parameters, such as #people , #/people/search or #/people/edit/12343 . 例如,如果散列包含多个级别和参数,例如#people#/people/search#/people/edit/12343

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

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