在递归聚合物组件中插入新子组件时,无法读取未定义的属性“concat”
Cannot read property ‘concat’ of undefined when inserting new child into recursive Polymer component
我遇到了递归聚合物组件的麻烦,该组件有一个用于插入新元素的按钮。它有add
链接用于插入元素。当单击我最初定义的元素时,它们可以工作,但对于由add插入的元素则失败-我得到一个错误Cannot read property ‘concat’ of undefined
。
触发错误,单击add
链接,插入节点成功,然后在插入节点上再次单击add
。这将失败并显示错误。
<script src="https://www.polymer-project.org/1.0/components/webcomponentsjs/webcomponents.min.js?20150925"></script>
<link rel="import" href="https://rawgit.com/Polymer/polymer/4c94736fac6681e84ec8c00da53484c5d3c2226b/polymer.html">
<template is="dom-bind">
<test-insert></test-insert>
</template>
<dom-module id="test-insert">
<template>
<test-recurse object="[[_object]]"></test-recurse>
</template>
</dom-module>
<dom-module id="test-recurse">
<template>
<style>
a { color: blue; text-decoration: underline; cursor: pointer; padding-left: 1em;}
</style>
Label: <span>[[object.label]]</span> <a on-click="_pushNewElement">add</a>
<ul>
<template is="dom-repeat" items="[[object.children]]">
<li><test-recurse object="[[item]]"></test-recurse></li>
</template>
</ul>
</template>
</dom-module>
<script>
(function () {
Polymer({
is: 'test-insert',
properties: {
_object: Object
},
ready: function () {
this._object = {
label: 'a1',
children: [
{ label: 'b1', children: [] },
{ label: 'b2', children: [{label: 'c1', children: []}] }
]
};
}
});
var count = 0;
Polymer({
is: 'test-recurse',
properties: {
object: Object
},
_pushNewElement: function () {
var newLabel = {label: 'Object ' + (++count)};
console.log({newLabel: newLabel}, '_pushNewElement');
this.object.children = this.object.children || [];
this.unshift('object.children', newLabel);
}
});
}());
</script>
任何帮助都非常感谢!
你只需要修改一行:
this.set('object.children', this.object.children || []);
children
数组未正确创建
相关文章:
- 组件生命周期问题/无法处理未定义的问题
- GoogleMap JSX组件中未定义的变量
- Ember服务在注入组件并在计算属性中使用后是未定义的
- ReactJS中未定义组件
- 使用AMD时未定义淘汰组件视图模型
- 传递给Angular2组件的字符串始终未定义
- 尝试在组件之间传递数据时未定义
- 未定义编码 URI 组件
- 角度2 - 未定义组件
- Vue.js 使用 Vuex 和自定义组件,@click方法输出未定义
- JavaScript 中介器模式;组件名称未定义
- 嵌入 API 自定义组件:未捕获类型错误:无法读取未定义的属性“ViewSelector2”
- 将控制器操作传递给EmberJS中的组件会返回未定义的结果
- 反应:“this”在组件函数中是未定义的
- Ember”;这个“;组件中未定义
- 在递归聚合物组件中插入新子组件时,无法读取未定义的属性“concat”
- 组件对话框无法在IE中运行(无法获取属性'clear'未定义或空引用)
- ReactJS: '类型未定义'加载组件时
- React组件方法this.state.myState从child返回后未定义
- React Redux - 不能使用将组件的方法传递给子组件 - 未定义的错误