我也尝试过,但似乎没有将元素作为要在 dom-repeat 中呈现的项目传递。
根据问题的复杂程度和要渲染的内容,我只能想到两种解决方案。
1) 这里到处都是 dom-if 元素,看起来很乱
<script>
Polymer({
is: 'your-element',
properties: {
myItems: {
type: Array,
value: [
{ type: 'checkbox', other: 'something', },
{ type: 'button', other: 'something else', },
]
},
},
isCheckbox: item => item.type === 'checkbox',
isButton: item => item.type === 'button',
});
</script>
<template is="dom-repeat" items="[[myItems]]">
<div>
<template id="dom-if" if="{{isCheckbox(item)}}">
<input type="checkbox" other="{{item.other}}" />
</template>
<template id="dom-if" if="{{isButton(item)}}"">
<paper-button other="{{item.other}}" ></paper-button>
</template>
</div>
</template>
2) 这会在你的外部和内部 dom 之间留下一个烦人的自定义重复元素,并且会使得访问呈现的元素变得更加困难
<dom-module id="custom-repeat">
<template>
</template>
</dom-module>
<custom-repeat array="{{anArrayOfElementProperties}}"></custom-repeat>
<script>
HTMLImports.whenReady(function () {
Polymer({
is: 'custom-repeat',
properties: {
items: {
type: Array,
// a default value just for an example
value: [
{name: 'paper-fab', icon: 'add' },
{name: 'iron-icon', icon: 'star' },
{name: 'paper-fab', icon: 'alarm' },
{name: 'iron-icon', icon: 'apps' },
],
observer: 'itemsChanged',
},
},
itemsChanged: function(items) {
// Delete all children
// Add new children based on items
},
});
});
</script>
示例 2 显示为here
我不太喜欢这两个答案,但希望它能让你渡过难关,直到有人找到一种方法来改变 dom-repeat 元素的呈现方式以允许传递实际节点,或者了解何时传递对 dom-的引用节点。