关于vue.js:vfor-and-slotscoped报错问题

9次阅读

共计 823 个字符,预计需要花费 3 分钟才能阅读完成。

可能会这么写

<a-table>
  <span v-for="(item, index) in header" :key="index" :slot="item.dataIndex" slot-scope="text">
    {{text}}
  </span>
</a-table>

然而这样子会报错,因为 v -for 和 slot-scope 在同一级

 报错信息:Ambiguous combined usage of slot-scope and v-for on (v-for takes higher priority). Use a wrapper <template> for the scoped slot to make it clearer.

提醒在外边包一层 < template >,于是可能改成上面这样,然而也会报错

<a-table>
  <template v-for="(item, index) in header" :key="index">
    <span :slot="item.dataIndex" slot-scope="text">
      {{text}}
    </span>
  </template>
</a-table>
 报错信息:<template> cannot be keyed. Place the key on real elements instead.

提醒 < template >template 不能写 key,即便没有这个错,表格数据也不会渲染进去,因为这一层没有 slot,应该说 slot 应该是放最里面,同时把:key 放外面


改成如下

<a-table>
  <template v-for="(item, index) in header" :slot="item.dataIndex" slot-scope="text">
    <span :key="index">
      {{text}}
    </span>
  </template>
</a-table>

以上解决问题

原文:https://blog.csdn.net/Cookysurongbin/article/details/101310442

正文完
 0