简体   繁体   English

聚合物路线:内部路线不反映

[英]Polymer routing: inner routes not reflecting

I am very new to Polymer and have been reading up and trying out a few things to get some hands-on experience. 我对PolymerPolymer ,一直在阅读并尝试一些方法来获得一些实践经验。

I decided to have Polymer as the views platform to a NodeJS (with ExpressJS as middleware) driven app that will serve out some media like videos, music and pictures. 我决定将Polymer用作NodeJS (以ExpressJS作为中间件)驱动的应用程序的视图平台,该应用程序将提供一些媒体,如视频,音乐和图片。 Incidentally, i am also new to NodeJS and Express but i have it working well enough to serve up to my Polymer views. 顺便说一句,我还是NodeJS和Express的新手,但我的工作情况足以满足我的Polymer视图。

I am having some issues with having inner routes reflect. 我在反映内部路线时遇到一些问题。 The address bar reflects the URL but my view does not change accordingly. 地址栏反映了该URL,但我的视图没有发生相应变化。 I seem to be missing something basic. 我似乎缺少一些基本知识。

I have the following flow: 我有以下流程:

  • App is served from a "nodepolymer" directory in my webserver (ie it is not a root app) Example URL: mywebroot/nodepolymer 通过我的Web服务器中的“ nodepolymer”目录提供应用程序(即它不是根应用程序)示例URL: mywebroot/nodepolymer
  • The homepage will have some selected pictures/text of available media. 主页将提供一些选定的可用媒体图片/文字。 Links to Videos/Music and Pictures will take the user to individual landing pages that will serve up a list of all available media of the selected type/category. 指向“视频/音乐和图片”的链接会将用户带到各个登陆页面,这些登陆页面将提供所选类型/类别的所有可用媒体的列表。 Example URL: mywebroot/nodepolymer/view-videos or mywebroot/nodepolymer/view-music etc etc 范例网址: mywebroot/nodepolymer/view-videosmywebroot/nodepolymer/view-music
  • Clicking on a particular media file will open into its own individual page Example URL: mywebroot/nodepolymer/view-music/view-media (no media identifier in the URL but the 'view-media' will be common across categories) 单击特定的媒体文件将打开到其自己的单独页面中。示例URL: mywebroot/nodepolymer/view-music/view-media (URL中没有媒体标识符,但“ view-media”在各个类别中是通用的)
  • Might go even one further level further (i understand the nesting is deep already but i may have the need) and have a page/view that serves as a function-only view. 甚至可以再上一层(我知道嵌套已经很深了,但是我可能有需要),并且页面/视图可以用作仅功能的视图。 Example URL: mywebroot/view-pictures/view-media/create-meme or mywebroot/view-videos/view-media/extract-audio etc etc. 范例网址: mywebroot/view-pictures/view-media/create-mememywebroot/view-videos/view-media/extract-audio等。

The root route and the first-level works fine ie the urls like mywebroot/view-videos/ or mywebroot/view-music/ . 根路由和第一层工作正常,例如,像mywebroot/view-videos/mywebroot/view-music/这样的URL。 The inner levels like mywebroot/view-videos/view-media does reflect in the address bar but the view does not switch. mywebroot/view-videos/view-media这样的内部级别确实反映在地址栏中,但视图不会切换。 It shows the mywebroot/view-videos/ view. 它显示了mywebroot/view-videos/视图。

The relevant files/code for getting the basic routing working are listed below. 下面列出了用于使基本路由正常工作的相关文件/代码。 I am using the latest versions of Polymer and the route is handled by app-route (app-route#^0.9.2). 我使用的是最新版本的Polymer,该路由由app-route (app-route#^ 0.9.2)处理。 Thanks in for any pointers. 感谢您的指导。

My index.html file: 我的index.html文件:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">
<title>My App</title>
<meta name="description" content="My App description">
<link rel="icon" href="/nodepolymer/images/favicon.ico">
<link rel="manifest" href="/nodepolymer/manifest.json">
<script>
    window.Polymer = {
        dom: 'shadow',
        lazyRegister: true
    };
    (function() {
        'use strict';
        var onload = function() {
            if (!window.HTMLImports) {
                document.dispatchEvent(
                    new CustomEvent('WebComponentsReady', {
                        bubbles: true
                    })
                );
            }
        };
        var webComponentsSupported = (
            'registerElement' in document &&
            'import' in document.createElement('link') &&
            'content' in document.createElement('template')
        );
        if (!webComponentsSupported) {
            var script = document.createElement('script');
            script.async = true;
            script.src = '/nodepolymer/bower_components/webcomponentsjs/webcomponents-lite.min.js';
            script.onload = onload;
            document.head.appendChild(script);
        } else {
            onload();
        }
    })();
    if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
            navigator.serviceWorker.register('/nodepolymer/service-worker.js');
        });
    }
</script>
<link rel="import" href="/nodepolymer/src/my-app.html">
<style>
    body {
        margin: 0;
        font-family: 'Roboto', 'Noto', sans-serif;
        line-height: 1.5;
        min-height: 100vh;
        background-color: #eeeeee;
    }
</style>
</head>
<body>
<my-app></my-app>
</body>
</html>

The "my-app" element: “ my-app”元素:

<link rel="import" href="../bower_components/polymer/polymer.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="../bower_components/paper-tabs/paper-tabs.html">
<link rel="import" href="view-videos.html">
<link rel="import" href="view-music.html">
<link rel="import" href="view-home.html">
<link rel="import" href="my-icons.html">
<dom-module id="my-app">
  <template id="app">
    <app-location route="{{route}}"></app-location>
    <app-route route="{{route}}" pattern="/nodepolymer/:page" data="{{routeData}}" tail="{{tail}}"></app-route>
    <iron-pages selected="[[routeData.page]]" attr-for-selected="data-route" fallback-selection="404">
        <section data-route="">
            Home {{routeData.page}}, [[routeData.page]] <!-- To view values on screen, no values displayed on homepage-->
            </br>
            <a href="/nodepolymer/view-videos">View Videos.</a> | <a href="/nodepolymer/view-music">View Music.</a>
            <view-home data-route="view-home" route=""></view-home>
        </section>
        <view-videos data-route="view-videos" route="{{tail}}"></view-videos>
        <view-music data-route="view-music" route="{{tail}}"></view-music>
        <section data-route="404">
            Oops you hit a 404.
            <a href="/nodepolymer">Head back home.</a>
        </section>
    </iron-pages>
</template>
<script>
    Polymer({
        is: 'my-app',
        properties: {
            page: {
                type: String,
                reflectToAttribute: true,
                observer: '_pageChanged'
            }
        },
        observers: [
            '_routeChanged(route.*)',
            '_viewChanged(routeData.view)'
        ],
        _routeChanged: function(changeRecord) {
            console.log("changeRecord: " + JSON.stringify(changeRecord))
            if (changeRecord.path === 'path') {
                console.log('Path changed!');
            }
        },
        _viewChanged: function(view) {
            console.log("View Changed: " + view)
                // load data for view
        },
        _showPage404: function() {
            this.page = 'view404';
        }
    });
  </script>
</dom-module>

The "view-video" element: “观看视频”元素:

<link rel="import" href="view-media.html"> 
<dom-module id="view-videos">
  <template id="app">
    <app-location route="{{route}}"></app-location>
    <app-route route="{{route}}" pattern="/:vid" data="{{videoData}}" tail="{{subRoute}}"></app-route>
    <app-route route="{{subRoute}}" pattern="/:category" data="{{category}}" tail="{{subRoutes}}"></app-route>
    <app-route route="{{subRoutes}}" pattern="/:cat" data="{{cat}}" tail="{{subRout}}"></app-route>
    <iron-pages selected="[[videoData.vid]]" attr-for-selected="data-route" fallback-selection="404">
        <section data-route="{{videoData.vid}}">
            View Videos {{videoData.vid}}, [[videoData.vid]] ; {{category.category}}, [[category.category]] ; {{cat.cat}}, [[cat.cat]]
            <!--  displays the following text on the view-videos url: nodepolymer, nodepolymer ; view-videos, view-videos ; ,   -->
            <br />
            <a href="/nodepolymer">Home.</a> | <a href="/nodepolymer/view-videos/view-media">View Info for Media.</a>
            <!-- This link will actually be in a repeater and will display a standalone view of the media that is tapped(by passing on an identifier. For testing the routes i have it a simple link. I may later also have another level as "view-video/view-media/my-media-id" for some furhther functions related to the particular media reference). Unable to get this viw intercepted as it defaults to "view-videos" rather than "view-videos/view-media". The URL changes but i see information from "view-video" rather than from "view-media" -->
        </section>
        <view-media data-route="view-media" route="{{subRoute}}"></view-media>
        <section data-route="404">
            404
            <a href="/nodepolymer">Head back home.</a>
        </section>
    </iron-pages>
</template>
<script>
    Polymer({
        is: 'view-videos',
        properties: {
            page: {
                type: String,
                reflectToAttribute: true,
                observer: '_pageChanged'
            }
        },
        observers: [
            '_routeChanged(route.*)',
            '_viewChanged(routeData.view)'
        ],
        _routeChanged: function(changeRecord) {
            console.log("changeRecord: " + JSON.stringify(changeRecord))
            if (changeRecord.path === 'path') {
                console.log('Path changed!');
            }
        },
        _viewChanged: function(view) {
            console.log("View Changed: " + view)
                // load data for view
        },
        _showPage404: function() {
            this.page = 'view404';
        }
    });
 </script>
</dom-module>

The "view-media" element: “查看媒体”元素:

<dom-module id="view-media">
<template id="appbb">
    <app-location route="{{rout}}"></app-location>
    <app-route route="{{rout}}" pattern="/:pg" data="{{rouData}}" tail="{{tal}}"></app-route>
    <iron-pages selected="[[rouData.pg]]" attr-for-selected="data-route" fallback-selection="404">
        <section data-route="">
            View Media {{rouData.page}}, [[rouData.pg]] <!-- Displays the following data (but view does not switch): nodepolymer, nodepolymer ; view-videos, view-videos ; view-media, view-media  -->
            <a href="/nodepolymer/view-videos">View Videos.</a><br />
            <a href="/nodepolymer/view-music">View Music.</a>
        </section>
        <section data-route="404">
            Oops you hit a 404.
            <a href="/nodepolymer">Head back home.</a>
        </section>
    </iron-pages>
</template>
<script>
    Polymer({
        is: 'view-media',
        properties: {
        },
        observers: [
        ],
        _routeChanged: function(changeRecord) {
         },

        _viewChanged: function(view) {
         },
        _showPage404: function() {
            this.page = 'view404';
        }
    });
 </script>
</dom-module>

I did manage to find a solution. 我确实设法找到了解决方案。 I went through the documentation and the videos on routes all over again and found that i would need to restructure my app a bit. 我遍历了路线上的文档和视频,发现我需要对应用程序进行一些重组。 I did that and I am now able to get several nested levels going. 我这样做了,现在我可以进行几个嵌套的关卡了。 One immediate issue was that i seemed to have <app-location> defined all over. 一个直接的问题是,我似乎已经定义了<app-location> I am now defining it just once. 我现在只定义一次。 But a clearer structure was the main requirement. 但是,更清晰的结构是主要要求。

Some useful links(besides the official documentation and videos) for people beginning out on routes in Polymer: 一些有用的链接(除了官方文档和视频),对于那些开始使用Polymer路线的人们来说:

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

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