我是Polymer
的新手,并且一直在阅读并尝试了一些事情来获得一些实践经验。
我决定让Polymer作为NodeJS
(以ExpressJS
为中间件)驱动的应用程序的视图平台,该应用程序将提供视频,音乐和图片等媒体。顺便说一句,我也是NodeJS和Express的新手,但我的工作效果非常好,可以提供我的Polymer视图。
我有一些内部路线反映的问题。地址栏反映了URL,但我的视图没有相应更改。我好像缺少一些基本的东西。
我有以下流程:
mywebroot/nodepolymer
mywebroot/nodepolymer/view-videos
或mywebroot/nodepolymer/view-music
等等mywebroot/nodepolymer/view-music/view-media
(网址中没有媒体标识符,但'查看媒体'将在各个类别中通用)mywebroot/view-pictures/view-media/create-meme
或mywebroot/view-videos/view-media/extract-audio
等等。根路由和第一级工作正常,即mywebroot/view-videos/
或mywebroot/view-music/
等网址。像mywebroot/view-videos/view-media
这样的内部级别确实反映在地址栏中,但视图不会切换。它显示mywebroot/view-videos/
视图。
下面列出了获取基本路由工作的相关文件/代码。我使用的是最新版本的Polymer,路由由app-route
处理(app-route#^ 0.9.2)。感谢您的任何指示。
我的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>
&#34; my-app&#34;元素:
<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>
&#34; view-video&#34;元素:
<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>
&#34; view-media&#34;元素:
<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>
答案 0 :(得分:0)
我确实找到了解决方案。我再次浏览了路线上的文档和视频,发现我需要重新构建我的应用程序。我这样做了,我现在能够获得几个嵌套级别。一个直接的问题是我似乎已经定义了<app-location>
。我现在只定义一次。但更清晰的结构是主要要求。
一些有用的链接(官方文档和视频除外),适用于以聚合物路线开头的人: