如何将更改的数组数据从目标传递到 Polymer 2.0 中的主机元素?

How to pass changed Array data from Target to Host element in Polymer 2.0?

我在主机元素中有一个数组,该数组使用绑定传递给目标元素。现在,我想更改目标元素中的数组数据,以确保当我更改目标元素中的数组时,宿主元素中的数组数据也会更新。

这是您需要检查的步骤:

  1. 在子元素(在您的术语中:目标),您需要声明 属性 和 notify:true

    static get properties() {return { myArray: { type:Array, notify:true }}}

  2. 允许在父级(你的词:主机)与 curly brackets 类似的东西进行双向绑定:<child-elem my-array="{{myArray}}"></child-elem>

  3. 在子元素处,您需要使用以下一些内容修改数组,以便在父元素处观察到变化; this.push(path, item1, [..., itemN]) this.pop(path) this.unshift(path, item1, [..., itemN]) this.shift(path) this.splice(path, index, removeCount, [item1, ..., itemN])