我有以下元件,其簡單地具有一個屬性,該元件本身設置:如何共享在聚合物的兩個元件之間的數據2.0
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="test-element-1">
<template>
<style>
:host {
display: block;
}
</style>
<h2>Hello [[prop1]]!</h2>
</template>
<script>
/**
* `test-element-1`
* Test Element 1
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class TestElement1 extends Polymer.Element {
static get is() { return 'test-element-1'; }
static get properties() {
return {
prop1: {
type: String,
value: 'test-element-1',
notify: true,
readOnly: true
}
};
}
}
window.customElements.define(TestElement1.is, TestElement1);
</script>
</dom-module>
和我想能夠第二元件使用相同的數據:
<link rel="import" href="../polymer/polymer-element.html">
<dom-module id="test-element-2">
<template>
<style>
:host {
display: block;
}
</style>
<h2>Hello [[prop1]]!</h2>
</template>
<script>
/**
* `test-element-2`
* Test Element 2
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class TestElement2 extends Polymer.Element {
static get is() { return 'test-element-2'; }
static get properties() {
return {
prop1: {
type: String,
notify: false,
readOnly: false
}
};
}
}
window.customElements.define(TestElement2.is, TestElement2);
</script>
</dom-module>
我想測試元件2能夠從測試元件1獲得PROP1的價值:
<!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>test-element-2 demo</title>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../iron-demo-helpers/demo-pages-shared-styles.html">
<link rel="import" href="../../iron-demo-helpers/demo-snippet.html">
<link rel="import" href="../test-element-2.html">
<link rel="import" href="../../test-element-1/test-element-1.html">
<custom-style>
<style is="custom-style" include="demo-pages-shared-styles">
</style>
</custom-style>
</head>
<body>
<div class="vertical-section-container centered">
<h3>Basic test-element-2 demo</h3>
<demo-snippet>
<template>
<test-element-1 prop1="{{prop1Value}}"></test-element-1>
<test-element-2 prop1="{{prop1Value}}"></test-element-2>
</template>
</demo-snippet>
</div>
</body>
</html>
什麼是我做錯了?
你可以創建一個新組件,讓我們說測試元素包裝,包括他們兩個並保持同步的價值,並在你的網頁你會包括該包裝,而不是兩個單獨的元素 – mishu