我对我的定制聚合物元素有疑问。首先,我用简单的纸张输入制作了一个元素。我的问题是,我不知道,如何将这个元素用作“独立”元素。我的例子就在这个jsfiddle中。输入第一个输入“asd”并按Enter键,然后在第二个输入“asd”中输入。您可以看到,两个元素共享属性(在数组中找不到控制台日志“-1”,第二个日志将为“1”)
<!doctype html>
<html>
<head>
<title>2.0 preview elements</title>
<base href="http://polygit.org/polymer+v2.0.0-rc.4/webcomponentsjs+webcomponents+v1.0.0-rc.6/shadycss+webcomponents+1.0.0-rc.2/paper*+polymerelements+:2.0-preview/iron*+polymerelements+:2.0-preview/app*+polymerelements+:2.0-preview/neon*+polymerelements+:2.0-preview/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="paper-input/paper-input.html">
</head>
<body>
<input-test></input-test>
<input-test></input-test>
<dom-module id="input-test">
<template>
<paper-input value="{{aValue}}" on-keydown="_keydown"></paper-input>
</template>
<script>
window.addEventListener('WebComponentsReady', function() {
class InputTest extends Polymer.Element {
static get is() {
return 'input-test';
}
static get properties() {
return {
aValue: {
type: String,
value: ''
},
_inputStore: {
type: Array,
value: []
}
};
}
_keydown(event) {
const keyCode_enter = '13';
if (event.keyCode == keyCode_enter) {
console.log(this._inputStore.indexOf(this.aValue))
this.push('_inputStore', this.aValue);
}
}
}
customElements.define(InputTest.is, InputTest);
})
</script>
</dom-module>
</body>
</html>
我能做什么,拥有独立的财产?
谢谢!
答案 0 :(得分:2)
我找到了答案。
问题是数组的默认值声明。
_inputStore: {
type: Array,
value: function () {
return [];
}
}
此代码解决了这个问题。