Here, I'm trying to build a working demo实现@CaptainCodeman编写的this blog article中包含的代码 - 在Polymer 2.0中管理状态 - 超越父/子绑定 在没有Redux的分离DOM元素之间共享状态。
我收到以下错误。
run.plnkr.co/:5 GET https://cdn.rawgit.com/download/polymer-cdn/2.0.0/lib/webcomponentsjs/webcomponents-lite.min.js
run.plnkr.co/:1获取https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/my-options.html 500()
run.plnkr.co/:1获取https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/shared-styles.html 500()
MY-view2.html:26
中定义
未捕获的ReferenceError:
MyOptionsMixin未在my-view2.html:26
有没有人有这方面的工作演示?
我-options.html<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="../bower_components/paper-checkbox/paper-checkbox.html">
<dom-module id="my-options">
<template>
<style>
:host {
display: block;
padding: 16px;
}
h3, p {
margin: 8px 0;
}
</style>
<h3>Options</h3>
<p>
<paper-checkbox checked="{{ options.subscribe }}">Send Notifications</paper-checkbox>
</p>
</template>
<script>
(function() {
let optionsInstance = null;
class MyOptions extends Polymer.Element {
static get is() { return 'my-options'; }
static get properties() {
return {
options: {
type: Object,
value: () => ({
subscribe: false
})
},
subscribers: {
type: Array,
value: () => []
}
}
}
static get observers() {
return [
'optionsChanged(options.*)'
]
}
constructor() {
super();
if (!optionsInstance) optionsInstance = this;
}
register(subscriber) {
this.subscribers.push(subscriber);
subscriber.options = this.options;
subscriber.notifyPath('options');
}
unregister(subscriber) {
var i = this.subscribers.indexOf(subscriber);
if (i > -1) this.subscribers.splice(i, 1)
}
optionsChanged(change) {
for(var i = 0; i < this.subscribers.length; i++) {
this.subscribers[i].notifyPath(change.path);
}
}
}
window.customElements.define(MyOptions.is, MyOptions);
MyOptionsMixin = (superClass) => {
return class extends superClass {
static get properties() {
return {
options: {
type: Object
}
}
}
connectedCallback() {
super.connectedCallback();
optionsInstance.register(this);
}
disconnectedCallback() {
super.disconnectedCallback();
optionsInstance.unregister(this);
}
}
}
}());
</script>
</dom-module>
我-view2.html
<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="my-options.html">
<link rel="import" href="shared-styles.html">
<dom-module id="my-view2">
<template>
<style include="shared-styles">
:host {
display: block;
padding: 10px;
}
</style>
<div class="card">
<div class="circle">2</div>
<h1>View Two</h1>
<p>Ea duis bonorum nec, falli paulo aliquid ei eum.</p>
<p>Id nam odio natum malorum, tibique copiosae expetenda mel ea.Detracto suavitate repudiandae no eum. Id adhuc minim soluta nam.Id nam odio natum malorum, tibique copiosae expetenda mel ea.</p>
<p>Send notifications option is: <b>[[ options.subscribe ]]</b></p>
</div>
</template>
<script>
class MyView2 extends MyOptionsMixin(Polymer.Element) {
static get is() { return 'my-view2'; }
}
window.customElements.define(MyView2.is, MyView2);
</script>
</dom-module>
编辑:Working Demo(已接受的答案分叉和编辑)
答案 0 :(得分:1)
run.plnkr.co/:5 GET https://cdn.rawgit.com/download/polymer-cdn/2.0.0/lib/webcomponentsjs/webcomponents-lite.min.js
您的<script>
代码会导入不存在的文件:
https://cdn.rawgit.com/download/polymer-cdn/2.0.0/lib/webcomponentsjs/webcomponents-lite.min.js
我会使用在Plunker其余部分中使用的相同polygit.org
网址:
https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/
此外,脚本名称不再包含min
。实际文件名为webcomponents-lite.js
。 <script>
标记应如下所示:
<script src="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/webcomponentsjs/webcomponents-lite.js"></script>
run.plnkr.co/:1获取https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/my-options.html 500()
run.plnkr.co/:1获取https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/shared-styles.html 500()
my-view2.html
将<base href>
设置为polygit.org
,然后尝试导入my-options.html
和shared-styles.html
,这两者都只存在于Plunker沙箱中:
<!-- my-view2.html -->
<base href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/">
<link rel="import" href="polymer/polymer-element.html">
<link rel="import" href="my-options.html"> <!-- https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/my-options.html -->
<link rel="import" href="shared-styles.html"> <!-- https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/shared-styles.html -->
此处导入的唯一需要来自polygit.org
的文件是polymer-element.html
,因此您可以移除<base>
代码并将网址前缀移至该导入:
<link rel="import" href="https://polygit.org/polymer+v2.0.0/shadycss+webcomponents+1.0.0/components/polymer/polymer-element.html">
my-view2.html:26未捕获的ReferenceError: MyOptionsMixin未在my-view2.html:26
中定义
my-view2
未声明/实例化my-options
,因此永远不会执行定义script
的{{1}}。您只需在MyOptionsMixin
模板中声明,即可解决此问题:
my-view2