Я использую Polymer 2.0 и хочу динамически вставить элемент компонента в DOM, для которого будут установлены правильные привязки.
<child-component collection="[[ parentCollection ]]"></child-component>
В этом примере у компонента child-component
есть свойство, называемое коллекцией, это свойство collection
должно быть связано со свойством parentCollection
родительского компонента (которое является простомассив чисел).
const node = document.createElement('child-component');
node.setAttribute('collection', '[[ parentCollection ]]');
this.$.container.appendChild(node);
Вышеуказанное не работает.Установка его как innerHTML (например, '<child-component collection="[[ parentCollection ]]"></child-component>'
) также не работает.
Пример родительского компонента
class ParentComponent extends Polymer.Element {
static get is() { return 'parent-component'; }
static get properties() {
return {
parentCollection: {
type: Array,
value: [1,2,3,4,5],
}
};
}
}
customElement.define(ParentComponent.is, ParentComponent);
Пример дочернего компонента
class ChildComponent extends Polymer.Element {
static get is() { return 'child-component'; }
static get properties() {
return {
collection: {
type: Array,
value: [],
}
};
}
connectedCallback() {
super.connectedCallback();
// The desired log would be [1,2,3,4,5]
console.log(this.collection);
}
}
customElement.define(ChildComponent.is, ChildComponent);
Образец Dom Module
<dom-module id="parent-component">
<template>
<style></style>
<div id="container></div>
</template>
<script src="parent-component.js"></script>
</dom-module>
Я пытался изучить Polymer.Bind
и Polymer.PropertyEffects
миксин, но, похоже, никуда не могу добратьсяс этим.
Любая помощь будет высоко ценится.Спасибо!