Arrays 如何使用存储在自定义聚合物2.0元素属性中的对象
我想在polymer declare属性(数组)中存储一些对象,稍后我将使用这些对象 它将存储对象,但该对象的方法不会被激发 工作流Arrays 如何使用存储在自定义聚合物2.0元素属性中的对象,arrays,object,this,polymer-2.x,Arrays,Object,This,Polymer 2.x,我想在polymer declare属性(数组)中存储一些对象,稍后我将使用这些对象 它将存储对象,但该对象的方法不会被激发 工作流 页面加载时\u pageActive将调用 \u pageActive将所有对象存储到ObjectCollection 由于ObjectCollection已更改\u showalobject将调用 此.ObjectCollection将在\u showalobject 在此.ObjectCollection[0]中引发错误。\u测试不是函数 下面是我的代码
- 页面加载时
将调用\u pageActive
将所有对象存储到\u pageActive
ObjectCollection
- 由于
已更改ObjectCollection
将调用\u showalobject
将在此.ObjectCollection
\u showalobject
- 在此.ObjectCollection[0]中引发错误。\u测试不是函数
<dom-module id="my-search">
<template>
<style>
:host {
display: table;
width: 100%;
}
</style>
<paper-icon-button id="search" icon="search"></paper-icon-button>
</template>
<script>
class MySearch extends Polymer.Element {
static get is() { return 'my-search'; }
static get properties() {
return {
pageActive: {
type: String,
observer: '_pageActive'
},
ObjectCollection: {
type: Array,
notify: true,
readOnly: false,
observer: '_showAllObject'
}
}
_pageActive(){
var thisElement = this;
var element1 = this.$.search;
this.ObjectCollection = [{thisElement },{element1}];
}
_showAllObject(){
console.log(this.ObjectCollection);
this.ObjectCollection[0]._test();
}
_test(){
alert("testing...!");
}
}
}
window.customElements.define(MySearch.is, MySearch);
</script>
</dom-module>
:主持人{
显示:表格;
宽度:100%;
}
类MySearch扩展了Polymer.Element{
静态get是(){return'my search';}
静态获取属性(){
返回{
页面活动:{
类型:字符串,
观察员:''u pageActive'
},
对象集合:{
类型:数组,
通知:正确,
只读:false,
观察者:“\u showalobject”
}
}
_pageActive(){
var thisElement=此;
var element1=this.$.search;
this.ObjectCollection=[{thisElement},{element1}];
}
_showalobject(){
log(this.ObjectCollection);
此.ObjectCollection[0]。_test();
}
_测试(){
警报(“测试…!”;
}
}
}
window.customElements.define(MySearch.is,MySearch);
为什么\u test()
方法未作为this.ObjectCollection[0]
获取调用
这是对象吗
为了观察到物体的变化,请使用:
this.set('ObjectCollection', [thisElement, element1]);
而不是
this.ObjectCollection = [{thisElement },{element1}];
然后您的\u showalobject
函数将被激发