You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Repeater中自定义元素无法正常工作的技术求助

解决Polymer dom-repeat中自定义元素不工作的问题

先把你提供的代码格式化一下,方便排查:

<dom-module id="mythings-latest-values">
  <template>
    <ul>
      <template is="dom-repeat" items="[[mythings]]">
        <li>X <mythings-list-item item="{{item}}"></mythings-list-item></li>
        <li>[[item.length]] [[((item.length)-1)]] {{item[item.length-1].code}}: [[item[item.length-1].price]]</li>
      </template>
    </ul>
  </template>
  <script>
    class MythingsLatestValues extends Polymer.Element {
      static get is() { return 'mythings-latest-values'; }
      // 这里需要定义mythings属性,确保数据可被Polymer观察
      static get properties() {
        return {
          mythings: {
            type: Array,
            value: () => []
          }
        };
      }
    }
    customElements.define(MythingsLatestValues.is, MythingsLatestValues);
  </script>
</dom-module>

我整理了几个最可能导致mythings-list-item失效的原因,对应解决方法如下:

1. 自定义元素未正确引入或注册

这是最常见的问题:

  • 如果mythings-list-item是单独文件定义的,必须在当前组件顶部导入它:
    <link rel="import" href="mythings-list-item.html">
    
  • 检查mythings-list-item自身的代码是否正确注册,并且定义了item属性:
    <dom-module id="mythings-list-item">
      <template>
        <!-- 这里是元素的模板,比如展示item的内容 -->
        <span>[[item.code]]: [[item.price]]</span>
      </template>
      <script>
        class MythingsListItem extends Polymer.Element {
          static get is() { return 'mythings-list-item'; }
          static get properties() {
            return {
              // 必须显式定义item属性,确保能接收dom-repeat传递的值
              item: {
                type: Object,
                observer: '_itemChanged' // 可选,监听属性变化做逻辑处理
              }
            };
          }
          _itemChanged(newItem) {
            // 调试用:确认是否收到数据
            if (newItem) console.log('Received item:', newItem);
          }
        }
        customElements.define(MythingsListItem.is, MythingsListItem);
      </script>
    </dom-module>
    

2. 数据绑定与可观察性问题

  • 你用了item="{{item}}"双向绑定,但如果不需要修改item并回传值,改用单向绑定item="[[item]]"更稳妥,避免不必要的绑定开销。
  • 确保mythings数组的更新符合Polymer的可观察规则:不要直接用this.mythings.push(...),而是用this.push('mythings', newItem);如果是异步加载数据,要在数据就绪后再赋值给mythings。

3. 数据是否正确加载

先确认mythings有数据:在MythingsLatestValues的生命周期钩子中打印数据,比如:

connectedCallback() {
  super.connectedCallback();
  // 模拟异步加载数据
  setTimeout(() => {
    this.mythings = [
      [{code: 'A', price: 10}, {code: 'B', price: 20}],
      [{code: 'C', price: 30}, {code: 'D', price: 40}]
    ];
  }, 1000);
}

另外,你第二个<li>里用了{{item[item.length-1].code}}双大括号,Polymer中建议用[[...]]单向绑定(除非是输入类双向绑定场景),改成[[item[item.length-1].code]]更规范。

4. 模板渲染顺序问题

如果数据是异步加载的,可以给dom-repeat加个dom-if判断,确保数据就绪后再渲染:

<template is="dom-if" if="[[mythings.length]]">
  <template is="dom-repeat" items="[[mythings]]">
    <!-- 你的列表项内容 -->
  </template>
</template>

先从这几个方向排查,应该能解决自定义元素不工作的问题。

内容的提问来源于stack exchange,提问作者mcv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:32:47