我已按照聚合物指南创建新页面:https://www.polymer-project.org/2.0/start/toolbox/create-a-page。但是当我点击导航菜单项"新视图"我得到了#34;哎呀你打了404.回到家里。"而不是我的新观点。我试图编辑现有的页面,一切似乎都运行正常,但我的新页面不是。我的文件与教程完全一样,我经常检查它。
MY-app.html:
<!--
@license
Copyright (c) 2016 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
-->
<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-view1.html">
<link rel="lazy-import" href="my-view2.html">
<link rel="lazy-import" href="my-view3.html">
<link rel="lazy-import" href="my-new-view.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-drawer-layout fullbleed 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="view1" href="/view1">View One</a>
<a name="view2" href="/view2">View Two</a>
<a name="view3" href="/view3">View Three</a>
<a name="new-view" href="/new-view">New View</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>My App</div>
</app-toolbar>
</app-header>
<iron-pages
selected="[[page]]"
attr-for-selected="name"
fallback-selection="view404"
role="main">
<my-view1 name="view1"></my-view1>
<my-view2 name="view2"></my-view2>
<my-view3 name="view3"></my-view3>
<my-new-view name="new-view"></my-new-view>
<my-view404 name="view404"></my-view404>
</iron-pages>
</app-header-layout>
</app-drawer-layout>
</template>
<script>
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: String,
// 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 'view1' in that case.
this.page = page || 'view1';
// 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
var 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>
我的新-view.html:
<!-- Load the Polymer.Element base class -->
<link rel="import" href="../bower_components/polymer/polymer-element.html">
<dom-module id="my-new-view">
<!-- Defines the element's style and local DOM -->
<template>
<style>
:host {
display: block;
padding: 16px;
}
</style>
<h1>New viewwww</h1>
</template>
<script>
// Your new element extends the Polymer.Element base class
class MyNewView extends Polymer.Element {
static get is() { return 'my-new-view'; }
}
//Now, register your new custom element so the browser can use it
customElements.define(MyNewView.is, MyNewView);
</script>
</dom-module>
答案 0 :(得分:1)
接缝都很好,就像你指向polymer-project
的页面一样,但你唯一需要纠正的事情。如果您想使用lazy-import
导入自己的网页,则需要在<dome-module>
和<template>
之间移动这一行,例如:
...
<!--import all necessary library ABOVE as you did and PLUS below -->
<link rel="import" href="../lazy-imports-mixin.html">
...
<dom-module id="my-app">
<link rel="lazy-import" group="lazy" href="my-view1.html">
<link rel="lazy-import" group="lazy" href="my-view2.html">
<link rel="lazy-import" group="lazy" href="my-view3.html">
<link rel="lazy-import" group="lazy" href="my-new-view.html">
<link rel="lazy-import" group="lazy" href="my-view404.html">
<template>
...
并扩展Polymer.LazyImportsMixin
的这个元素:
class MyApp extends Polymer.LazyImportsMixin(Polymer.Element) {...
请参考here
中的lazy-load
语法
或者您可以简单地在同一个地方导入页面,但不要像lazy-import
那样导入:
<link rel="import" href="my-view1.html">
<link rel="import" href="my-view2.html">
<link rel="import" href="my-view3.html">
<link rel="import" href="my-new-view.html">
<link rel="import" href="my-view404.html">
答案 1 :(得分:1)
它实际上是导入的函数_pageChanged
,懒惰的导入内容只是为了欺骗linter。
话虽如此,我不明白为什么它不起作用,除非它是服务器的问题。
如果您浏览到我的my-new-view.html页面,那该404会不会?如果是这样,则不提供页面。还要在你的控制台中查看它是否会抛出错误。
答案 2 :(得分:0)
我猜错误在函数__Pagechanged和链接函数中。请参阅:这取决于您的网页名称我的页面&#39; ...我想您的页面应该是&#39; my-number_of_page_-view&#39;或重写开始代码