带有子分页的Polymer-App路由 [英] Polymer- App Route with Sub Paging

查看:69
本文介绍了带有子分页的Polymer-App路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已阅读: https://www.polymer-project. org/2.0/toolbox/case-study#routing 并且: https://github.com/PolymerElements/app-route

我想我只是对如何使用子路由组件感到困惑,但是我读过的文档和示例并没有真正使我明白.

I think I am just confused how to use the subrouting component, but the documents and example I have read hasn't really gotten me anywhere.

如果有人能为我指出房间里的大象,那就太好了.

If anyone could point out the elephant in the room for me that would be great.

我要达到什么目标? 我在my-matters.html上显示了一个项目列表(称为事项")(此页面当前调用了另一个自定义元素,称为"issues-list.html",该列表通过该列表) http://127.0.0.1:8081/matters

What am I trying to Achieve? I have a list of items(called matters) which I display on my-matters.html (this page currently calls another custom element called matters-list.html which brings through the list) http://127.0.0.1:8081/matters

我希望能够单击其中的一个并转到详细信息页面(我已经构建了这个名为"messages-details.html"的模板 EG: http://127.0.0.1:8081/matters/123

I want to be able to click on one of these and go to a details page (I've built this template called matters-details.html EG: http://127.0.0.1:8081/matters/123

问题:

如何处理my-matters.html上的路由 我的理解是,如果我添加一个按钮来访问href/matters/123,则my-matter.html页面上应该有一些应用程序路由逻辑,该逻辑知道要关闭问题列表并打开问题详细信息?

How do I handle the routing on the my-matters.html my understanding is that if I add a button to hit the href /matters/123 there should be some app route logic on the my-matter.html page that knows to close the matter-list and open the matter detail?

还是我要解决这个错误,需要添加两页 my-matters-list.html my-matters-details.html 并在my-app路由中在url/matters下使用两者?

Or am I going about this wrong and need to add two pages my-matters-list.html my-matters-details.html and in the my-app routing use both there under the url /matters ?

以下是我从入门工具包中获取的代码:my-app.html

Here is my code taken from the starter kit mostly: my-app.html

    <link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="../bower_components/app-layout/app-drawer/app-drawer.html">
<link rel="import" href="../bower_components/app-layout/app-drawer-layout/app-drawer-layout.html">
<link rel="import" href="../bower_components/app-layout/app-header/app-header.html">
<link rel="import" href="../bower_components/app-layout/app-header-layout/app-header-layout.html">
<link rel="import" href="../bower_components/app-layout/app-scroll-effects/app-scroll-effects.html">
<link rel="import" href="../bower_components/app-layout/app-toolbar/app-toolbar.html">
<link rel="import" href="../bower_components/app-route/app-location.html">
<link rel="import" href="../bower_components/app-route/app-route.html">
<link rel="import" href="../bower_components/iron-pages/iron-pages.html">
<link rel="import" href="../bower_components/iron-selector/iron-selector.html">
<link rel="import" href="../bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="my-icons.html">

<link rel="lazy-import" href="my-home.html">
<link rel="lazy-import" href="my-matters.html">
<link rel="lazy-import" href="my-view2.html">
<link rel="lazy-import" href="my-view3.html">
<link rel="lazy-import" href="my-view404.html">

<dom-module id="my-app">
  <template>
    <style>
      :host {
        --app-primary-color: #4285f4;
        --app-secondary-color: black;

        display: block;
      }

      app-drawer-layout:not([narrow]) [drawer-toggle] {
        display: none;
      }

      app-header {
        color: #fff;
        background-color: var(--app-primary-color);
      }

      app-header paper-icon-button {
        --paper-icon-button-ink-color: white;
      }

      .drawer-list {
        margin: 0 20px;
      }

      .drawer-list a {
        display: block;
        padding: 0 16px;
        text-decoration: none;
        color: var(--app-secondary-color);
        line-height: 40px;
      }

      .drawer-list a.iron-selected {
        color: black;
        font-weight: bold;
      }
    </style>

    <app-location
        route="{{route}}"
        url-space-regex="^[[rootPath]]">
    </app-location>

    <app-route
        route="{{route}}"
        pattern="[[rootPath]]:page"
        data="{{routeData}}"
        tail="{{subroute}}">
    </app-route>

    <app-route
    route="{{subroute}}"
    pattern="/:id"
    data="{{subrouteData}}">
</app-route>

    <app-drawer-layout fullbleed force-narrow narrow="{{narrow}}">
      <!-- Drawer content -->
      <app-drawer id="drawer" slot="drawer" swipe-open="[[narrow]]">
        <app-toolbar>Menu</app-toolbar>
        <iron-selector selected="[[page]]" attr-for-selected="name" class="drawer-list" role="navigation">
          <a name="home" href="[[rootPath]]home">Welcome</a>
          <a name="matters" href="[[rootPath]]matters">Matters</a>
          <a name="view2" href="[[rootPath]]view2">View Two</a>
          <a name="view3" href="[[rootPath]]view3">View Three</a>
        </iron-selector>
      </app-drawer>

      <!-- Main content -->
      <app-header-layout has-scrolling-region>

        <app-header slot="header" condenses reveals effects="waterfall">
          <app-toolbar>
            <paper-icon-button icon="my-icons:menu" drawer-toggle></paper-icon-button>
            <div main-title>LegalSuite Software</div>
          </app-toolbar>
        </app-header>

        <iron-pages
            selected="[[page]]"
            attr-for-selected="name"
            fallback-selection="view404"
            role="main">
          <my-home name="home"></my-home>
          <my-matters name="matters"></my-matters>
          <my-view2 name="view2"></my-view2>
          <my-view3 name="view3"></my-view3>
          <my-view404 name="view404"></my-view404>
        </iron-pages>
      </app-header-layout>
    </app-drawer-layout>
  </template>

  <script>
    // Gesture events like tap and track generated from touch will not be
    // preventable, allowing for better scrolling performance.
    Polymer.setPassiveTouchGestures(true);

    class MyApp extends Polymer.Element {
      static get is() { return 'my-app'; }

      static get properties() {
        return {
          page: {
            type: String,
            reflectToAttribute: true,
            observer: '_pageChanged',
          },
          routeData: Object,
          subroute: Object,
          // This shouldn't be neccessary, but the Analyzer isn't picking up
          // Polymer.Element#rootPath
          rootPath: String,
        };
      }

      static get observers() {
        return [
          '_routePageChanged(routeData.page)',
        ];
      }

      _routePageChanged(page) {
        // If no page was found in the route data, page will be an empty string.
        // Default to 'home/Welcome' in that case.
        this.page = page || 'home';

        // Close a non-persistent drawer when the page & route are changed.
        if (!this.$.drawer.persistent) {
          this.$.drawer.close();
        }
      }

      _pageChanged(page) {
        // Load page import on demand. Show 404 page if fails
        const resolvedPageUrl = this.resolveUrl('my-' + page + '.html');
        Polymer.importHref(
            resolvedPageUrl,
            null,
            this._showPage404.bind(this),
            true);
      }

      _showPage404() {
        this.page = 'view404';
      }
    }

    window.customElements.define(MyApp.is, MyApp);
  </script>
</dom-module>

my-matters.html: `

my-matters.html : `

<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="shared-styles.html">
<link rel="import" href="Matters/matter-list.html">
<link rel="import" href="Matters/matter-detail.html">
<dom-module id="my-matters">
<div>
  <template>
    <style include="shared-styles">
      :host {
        display: block;

        padding: 10px;
      }
    </style>

    <div class="card">
      <div class="circle">M</div>
      <h1>Matters</h1>
      <matter-list></matter-list>
      //example
      <matter-detail linkedmatterid="123"></matter-detail>
  </template>
</div>
  <script>
    class Matters extends Polymer.Element {
      static get is() { return 'my-matters'; }
         }

    window.customElements.define(Matters.is, Matters);
  </script>

</dom-module>

推荐答案

如果您必须具有/matters/123

1)从my-app.html中删除第二个app-route元素,并将其添加到my-matters.html

1) Remove the second app-route element from my-app.html and add it to my-matters.html

<app-route
    route="{{subroute}}"
    pattern="/:id"
    data="{{subrouteData}}">
</app-route>

2)更改my-app.html元素中的Iron-pages以包含路线:

2) Change the iron-pages in my-app.html element to include the route:

<my-matters name="matters" route="[[subroute]]"></my-matters>

3)将ID添加为my-matters.html的属性

3) Add the id as a property of my-matters.html

  static get properties() { return {
    id: {
      type: Number,
      value: ''
    },
    route: {
      type: Object
    },
    subrouteData: {
      type: Object
    }
  }

4)更新my-matters.html中的模板以显示详细信息并隐藏列表,即

4) Update the template in my-matters.html to show the details and hide the list, i.e.

<template is="dom-if" if="[[!_idIsDefined(subrouteData.id)]]">
      <matter-list></matter-list>
</template>
<template is="dom-if" if="[[_idIsDefined(subrouteData.id)]]">
      <matter-detail linkedmatterid="[[subrouteData.id]]"></matter-detail>
</template>

5)创建逻辑以对模板进行操作:

5) Create logic to operate on the template:

_idIsDefined(id){
  //There are probably ways to optimize this
  if(id){
     return true;
  }
  return false;
}

我建议使用 Polymer Shop App 来学习Polymer,它的代码在路由方面是如此丰富,观察者,数据绑定,并且是一个很好的起点.

I'd advise using the Polymer Shop App to learn Polymer, its code is so rich in routing, observers, data binding, and serves as a great starting point.

更新了第3步和第4步,以使用subrouteData中的id.

Updated steps 3 and 4 to use id from subrouteData.

这篇关于带有子分页的Polymer-App路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆