【问题标题】:Polymer 2.5.0 - dom-repeat not updatingPolymer 2.5.0 - dom-repeat 不更新
【发布时间】:2018-08-29 19:16:54
【问题描述】:

我一直在寻找解决问题的方法,遇到了 this post 和 this other post,但它并没有解决我的问题。

我想动态地将<li> 添加到<ul>,但是当我使用数组变异方法时它不起作用(我可以正确记录数组的更新)。

我尝试使用 JSON 数组而不是简单字符串,并尝试执行 this.notifyPath('peoples');,但仍然无法正常工作

static get properties() {
    return {
        peoples: {
            type: Array,
            value: []
        }
    }
}
_test2() {
    console.log(this.peoples);

}
_test() {
    this.push('peoples', 'test');
}
<div class="flex__item--top list">
    <ul id="namelist">
        <template is="dom-repeat" items={{peoples}}>
            <li>{{item}}</li>
        </template>
    </ul>
</div>

【问题讨论】:

  • 首先,使用临时数组将一些元素推送到数组中。然后使用this.set('people', tempArray),这将在people 数组上产生可观察到的变化,并将立即在dom-repeat 生效。

标签: javascript html dom polymer-2.x


【解决方案1】:

您可以通过调用render 方法强制dom-repeat 重新渲染。

render(): void

强制元素呈现其内容。通常渲染是 异步到一个挑衅的变化。这样做是为了提高效率 多次更改只会触发一次渲染。渲染方法 应该调用,例如,如果需要模板渲染 验证应用程序状态。

所以如果你的dom-repeat是这样的

<template id="list" is="dom-repeat" items={{peoples}}>
            <li>{{item}}</li>
</template>

你可以像这样从你的组件中强制渲染

_test() {
    this.push('peoples', 'test');
    this.$.list.render();
}

【讨论】:

  • 我不知道,谢谢,但它仍然不起作用。这真的很奇怪。我得到了错误:Uncaught TypeError: this.$.namelist.render is not a function。我编辑:_test() { this.push('peoples', { test: "test" }); this.$.namelist.render(); } 和:&lt;template id="namelist" is="dom-repeat" items={{peoples}}&gt;
  • 嘿,没关系,当我吃完午饭回来时,我重新安装了依赖项并且它工作了,即使没有渲染功能。谢谢你的时间:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多