简体   繁体   English

使用 Page.js 的 Polymer 应用程序路由部分

[英]Polymer app routing partials with Page.js

I'm using the Polymer Starter Kit and would like have each route's content in a separate file (/pages/games.html, /pages/movies.html, etc) but I can't find any examples of this.我正在使用 Polymer Starter Kit,并希望将每个路线的内容放在一个单独的文件中(/pages/games.html、/pages/movies.html 等),但我找不到任何此类示例。

Could someone point me in the right direction?有人能指出我正确的方向吗? Or is it not possible or recommended to implement routing like this?或者是不可能或不建议实现这样的路由?

You could approach this a lot of different ways (replacement of a holder in index.html at build time, swapping for a different router).您可以通过多种不同的方式来解决这个问题(在构建时替换 index.html 中的持有者,交换不同的路由器)。 One such approach would be to implement your files and then fetch() them into the DOM.一种这样的方法是实现您的文件,然后将它们 fetch() 放入 DOM。 This is sort of the approach that is used in the partials example that is outlined in page.js repo.这是 page.js 存储库中概述的部分示例中使用的一种方法。

So, let's modify iron-pages in index.html in the starter kit to have a loading section:所以,让我们修改 starter kit 中index.html中的iron-pages来增加一个加载部分:

<iron-pages attr-for-selected="data-route" selected="{{route}}">

  <!-- Block we'll load our partials into -->
  <section id="load" data-route="load"></section>

...

Then let's modify elements/routing.html to change our page.js.然后我们修改elements/routing.html来改变我们的page.js。 Let's route /test to our target load section:让我们将/test路由到我们的目标负载部分:

window.addEventListener('WebComponentsReady', function() {

  page('/test', function () {

    // iron-pages needs to show the proper section
    // in this case, our designated loading target
    app.route = 'load';

    // We include fetch.js polyfill in route.html for simplicity
    // 1. bower install fetch
    // 2. Add <script src="../../bower_components/fetch/fetch.js"></script> to routing.html
    fetch('/pages/test.html')
      .then(function(response) {
        return response.text()
      }).then(function(body) {
        document.querySelector('#load').innerHTML = body;
      });
  });

  ...

We could then implement any amount of pages we want in routing.html that would load our html pages as needed.然后我们可以在routing.html中实现我们想要的任意数量的页面,这些页面将根据需要加载我们的html 页面。

Note, this basic approach doesn't take into account caching the response (back/forward would trigger the request again, which you probably don't want from a performance standpoint) and we're not catching our errors in the above example.请注意,这种基本方法没有考虑缓存响应(后退/前进会再次触发请求,从性能的角度来看,您可能不希望这样做)并且我们没有在上面的示例中捕获我们的错误。 But it's one such approach.但这是一种这样的方法。

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

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