【发布时间】:2022-12-13 03:24:56
【问题描述】:
例如标题,我尝试创建一个菜单,我使用 v-for 来获取数组项和键。然后我需要按键来创建第二层菜单。
const localSite = ref('us')
const products = ref({})
const softwares = ref({})
const menuShow = ref({})
const menuTxt = ref({
"us": {
"products": "Products",
"softwares": "Software"
}
})
<div id="menu-wrapper">
<div class="flex">
<div v-for="( item, dataKey ) in menuTxt[localSite]" class="menu-item">
<button class="text-white" :data-category="dataKey" @click="slideDown(dataKey)">
{{ item }}
<font-awesome-icon icon="angle-down" />
</button>
<div class="text-box menu-content">
<p v-for="itemA in dataKey">{{itemA}}</p>
</div>
</div>
</div>
</div>
我认为 'dataKey' 是一个变量,它是 vue 数据对象。 但我总是会得到错误的结果。 我该怎么办?
【问题讨论】:
-
迭代对象有点棘手,我建议使用数组并将键添加为属性。另一种选择是使用地图:designcise.com/web/tutorial/how-to-iterate-over-a-map-in-vuejs