我正在使用 Vue.js 生成 div,我想使用 JSON 文件中的数据来生成 div。
它不一定必须来自 JSON,但那会更好,我只需要为下面 html 中的每个 div 实例创建一个唯一的 id。
为每个 div 创建唯一 ID 的最佳方法是什么?
HTML
<ul>
<li v-for="(member, index) in cyclists" :key="index" class="col-md-4 inview fromBottomIn" data-in="fromBottomIn" data-out="fromBottomOut">
<div class="cycling__card">
<div class="cycling__rider-container">
<span class="cyclist__name">{{member.name}}</span>
</div>
<!-- Need to add id's to the div below -->
<div id={{member.name}}>
</div>
</div>
</li>
</ul>
JSON
"cyclists": [
{
"name": "Jason",
"position": "CEO",
},
{
"name": "Mike",
"position": "Digital Strategist",
},
{
"name": "Tom",
"position": "Vice President, Product Management",
},
{
"name": "Jeff",
"position": "Senior Director, Creative",
},
}
原文由 SBT23434 发布,翻译遵循 CC BY-SA 4.0 许可协议
我在这种情况下使用 uuid ,您需要将 json 数据合并到另一个具有新 id 的对象,因此示例:
在
computed
使用扩展运算符将新 ID 与当前 JSON 数据合并为新 ID,vue-uuid
来自 uuid 库,v4 与生成 ID 相关