简体   繁体   English

如何使用page.js在位于网站内部页面的Polymer应用程序中实例化路由

[英]How to instantiate route in a Polymer app located on an internal page of a site using page.js

I'm attempting to run a Polymer app on an internal page of a site and having an issue setting the initial route. 我试图在网站的内部页面上运行Polymer应用程序,但设置初始路径时遇到问题。 The app is located at 127.0.0.1:8080/hardware on local dev. 该应用程序位于本地开发人员的127.0.0.1:8080/hardware中。

My custom Element looks like this: 我的自定义元素如下所示:

 <link rel="import" href="../../polymer/polymer.html" /> <link rel="import" href="../../iron-flex-layout/iron-flex-layout.html" /> <link rel="import" href="../../iron-pages/iron-pages.html" /> <link rel="import" href="../../iron-selector/iron-selector.html" /> <dom-module id="category-select"> <style> :host {} iron-selector { @apply(--layout-horizontal); @apply(--layout-wrap); @apply(--layout-justified); } .fig-wrap { @apply(--layout-flex); margin: 0 .8rem; min-width: 150px; } .fig-wrap figure img { margin: 0 auto; display: block; } figcaption { padding-top: 5px; } .fig-wrap figure figcaption h3 { font-size: 1rem; text-align: center; } </style> <template> <!--Allows selection by dom elements produced in template repeater--> <h1>Hey We are looking at the <span>{{category}}</span>category</h1> <a data-route="home" href="/hardware">home</a> <iron-pages attr-for-selected="data-route" selected="{{route}}"> <section data-route="home"> <h1>Home route</h1> <a data-route="catz" href="/catz">CATZ</a> <iron-selector attr-for-selected="name" selected="{{category}}"> <!-- Acts as a for each loop to repeat--> <template is="dom-repeat" items="{{categories}}"> <div name$="{{item.name}}" class="fig-wrap"> <!--<a href="{{item.link}}">--> <figure> <img src="{{item.img}}" alt="" /> <figcaption> <h3>{{item.name}}</h3> </figcaption> </figure> <!--</a>--> </div> </template> </iron-selector> </section> <section data-route="catz"> <h1>Category route</h1> </section> </iron-pages> </template> </dom-module> <script> Polymer({ is: 'category-select', ready: function() { this.categories = [{ name: 'Anchors', img: 'https://example.com/img/hardware/anchor.jpg', link: 'https://www.example.org/', products: [{ name: 'Anchor', image: 'path/to/image.jpg', steel: '316 Stainless Steel', details: ['precision cut', 'polished from grade 316 stainless steel', 'suitable for both sailboats and powerboats', 'these anchors range in size from 25 lbs to 150 lbs'], options: [{ size: '25 pounds', price: '3041.75', code: '32442' }, { size: '35 pounds', price: '4203.25', code: '4234' }, { size: '45 pounds', price: '5146.25', code: '34231' }, { size: '60 pounds', price: '6842.50', code: '1224' }, { size: '80 pounds', price: '8912.50', code: '1234' }, { size: '100 pounds', price: '11183.75', code: '1234' }, { size: '150 pounds', price: '14547.50', code: '1234' }] }, { name: 'Cast Iron Grapnel Folding Anchor', image: 'string', steel: 'Cast Iron Galvanized', details: ['products details', 'put them in a list', 'a pretty ul'], options: [{ size: 'string', price: 'string', code: 'string' }] }, { name: '360° Anchor Swivel', image: 'string', steel: '17-4 PH Stainless Steel', details: ['products details', 'put them in a list', 'a pretty ul'], options: [{ size: 'string', price: 'string', code: 'string' }] }, { name: 'Universal Anchor Swivel', image: 'string', steel: '17-4 PH Stainless Steel', details: ['products details', 'put them in a list', 'a pretty ul'], options: [{ size: 'string', price: 'string', code: 'string' }] }, { name: 'Bow Chain Stopper', image: 'string', steel: '316 Stainless Steel', details: ['products details', 'put them in a list', 'a pretty ul'], options: [{ size: 'string', price: 'string', code: 'string' }] }, { name: 'Anchor Chain & Spliced Rope Anchor Line', image: 'string', steel: '316L Stainless Steel & 3 Strand Twist', details: ['products details', 'put them in a list', 'a pretty ul'], options: [{ size: 'string', price: 'string', code: 'string' }] }] }, { name: 'Chain', img: 'https://example.com/img/hardware/chain.jpg', link: 'https://www.example.com/' } ]; }, properties: { category: { computed: '_computeCategory(categories, name)' } }, _computeCategory: function(categories, name) { return categories[name]; } }); </script> 

The custom element is wrapped in a <template is="dom-bind" id=app> on the page. 自定义元素包装在页面上的<template is="dom-bind" id=app>

My routing.html is called from elements.html which looks like this: 我的routing.html是从elements.html中调用的,它看起来像这样:

 <!--Elements from polymer--> <link rel="import" href="../iron-selector/iron-selector.html"/> <link rel="import" href="../iron-image/iron-image.html"/> <link rel="import" href="../paper-button/paper-button.html"/> <link rel="import" href="../paper-material/paper-material.html"/> <link rel="import" href="../iron-flex-layout/iron-flex-layout.html"/> <!--Custom Elements--> <!-- app-theme goes here--> <link rel="import" href="../elements/category-select/category-select.html"/> <!--Configure routes--> <link rel="import" href="routing.html"/> 

And routing.html looks like this: 而routing.html看起来像这样:

 <!-- @license Copyright (c) 2015 The Polymer Project Authors. All rights reserved. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as part of the polymer project is also subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt --> <script src="../page/page.js"></script> <script> window.addEventListener('WebComponentsReady', function() { var app = document.querySelector('#app'); // We use Page.js for routing. This is a Micro // client-side router inspired by the Express router // More info: https://visionmedia.github.io/page.js/ page('/hardware', function () { app.route = 'home'; }); page('/catz', function () { app.route = 'catz'; }); // // page('/users', function () { // app.route = 'users'; // }); // // page('/users/:name', function (data) { // app.route = 'user-info'; // app.params = data.params; // }); // page('/categories', function () { // app.route = 'categories'; // }); // // page('/categories/:name', function (data) { // app.route = 'category-info'; // app.params = data.params; // }); // // // page('/contact', function () { // app.route = 'contact'; // }); // page('/anchors', function () { // app.route = 'anchors'; // }); // add #! before urls page({ hashbang: true }); }); </script> 

I call this script at the bottom of the page: 我在页面底部调用此脚本:

 (function(document) { 'use strict'; // Grab a reference to our auto-binding template // and give it some initial binding values var app = document.querySelector('#app'); app.route = 'home'; })(document); 

So as a note, before adding the <iron-pages> element my iron selector and dom-repeat template functioned. 因此,请注意,在添加<iron-pages>元素之前,我的铁选择器和dom-repeat模板已运行。

My issue now is that when <iron-pages attr-for-selected="data-route" selected="{{route}}"> has its selected set to {{route}} the iron-pages will not display any of the sections but displays a url of hardware/#!/hardware . 现在的问题是,当<iron-pages attr-for-selected="data-route" selected="{{route}}">的选择设置为{{route}}时,iron-pages将不会显示任何这些部分,但显示的网址为hardware/#!/hardware

When I set iron-pages selected attribute manually to selected="home" the home view displays and iron-selector and dom-repeat both function. 当我将Iron-pages selected属性手动设置为selected="home"将显示主页视图,并且铁选择器和dom-repeat都起作用。 Yet when I click on the link to the route catz the view remains the same while the url changes. 但是,当我单击指向路径catz的链接时,URL更改时视图仍然相同。

console.log(app.route) in the dev tools returns home at all points. console.log(app.route)在开发者工具返回home在所有点。 I've done my best to emulate the Polymer 1.0 Starter Kit's routing methods but cannot get it to work. 我已尽力模仿Polymer 1.0入门工具包的路由方法,但无法使其正常工作。

So, how do I go about setting the route to home initially, but then allowing it to change on user interaction? 那么,我如何首先设置到家的路线,然后在用户交互时更改它呢?

For anyone else who is stuck on this. 对于任何坚持这一点的人。 This Github issue hints at replacing 这个Github问题暗示了替换

page({
  hashbang: true
});

with

page.base('#!')

This seems to help the initial load problem but then you run into issues where clicking links like /hardware don' use hashbangs anymore :\\ 这似乎可以解决最初的加载问题,但随后您会遇到以下问题:单击/hardware类的链接不再使用hashbangs:\\

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

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