英文:
what does _ mean as first parameter in for-loop
问题
根据此链接,当我浏览和检查下面发布的代码时,吸引了我的注意力
v-for="(_, tab) in tabs"
在下面的代码中显示。
我想知道在 for-loop
中的第一个参数 _
是什么意思,请。
代码:
<script setup>
import Home from './Home.vue'
import Posts from './Posts.vue'
import Archive from './Archive.vue'
import { ref } from 'vue'
const currentTab = ref('Posts')
const tabs = {
Home,
Posts,
Archive
}
</script>
<template>
<div class="demo">
<button
v-for="(_, tab) in tabs"
:key="tab"
:class="['tab-button', { active: currentTab === tab }]"
@click="currentTab = tab"
>
{{ tab }}
</button>
<component :is="tabs[currentTab]" class="tab"></component>
</div>
</template>
英文:
referring to this link, and as i peruse and inspect the code posted below,attracted my attention the
v-for="(_, tab) in tabs"
as shown in the code below.
i would like to know what does _
mean as first parameter in a for-loop
please
code:
<script setup>
import Home from './Home.vue'
import Posts from './Posts.vue'
import Archive from './Archive.vue'
import { ref } from 'vue'
const currentTab = ref('Posts')
const tabs = {
Home,
Posts,
Archive
}
</script>
<template>
<div class="demo">
<button
v-for="(_, tab) in tabs"
:key="tab"
:class="['tab-button', { active: currentTab === tab }]"
@click="currentTab = tab"
>
{{ tab }}
</button>
<component :is="tabs[currentTab]" class="tab"></component>
</div>
</template>
答案1
得分: 2
这只是一个未使用的变量的约定。
您真正使用的是Vue的v-for
与对象一起使用
v-for="(value, key) in collection"
但在您的实现中:
collection
->tabs
value
->_
key
->tab
如果这有助于您更好地理解它,考虑一下这个
<button
v-for="tabName in Object.keys(tabs)"
:key="tabName"
:class="['tab-button', { active: currentTab === tabName }]"
@click="currentTab = tabName"
>
{{ tabName }}
</button>
英文:
It's just convention for an unused variable.
What you're really using is Vue's v-for
with an Object
v-for="(value, key) in collection"
But in your implementation:
collection
->tabs
value
->_
key
->tab
If it helps you understand it better, consider this
<button
v-for="tabName in Object.keys(tabs)"
:key="tabName"
:class="['tab-button', { active: currentTab === tabName }]"
@click="currentTab = tabName"
>
{{ tabName }}
</button>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论