我正在尝试学习 vue.js。我正在添加元素列表,我想将 class="active"
仅添加到 for 循环中的第一个元素。以下是我的代码:
<div class="carousel-inner text-center " role="listbox">
<div class="item" v-for="sliderContent in sliderContents">
<h1>{{ sliderContent.title }}</h1>
<p v-html="sliderContent.paragraph"></p>
</div>
</div>
所以第一个元素应该是这样的:
<div class="item active">
<h1>WELCOME TO CANVAS</h1>
<p>Create just what you need for your Perfect Website. Choose from a wide<br>range of Elements & simple put them on your own Canvas</p>
</div>
我能够获取数据,因此一切正常。
以下是我的脚本代码:
<script>
export default{
data() {
return {
sliderContents: [
{
title: "WELCOME TO CANVAS",
paragraph: "Create just what you need for your Perfect Website. Choose from a wide<br>range of Elements & simple put them on your own Canvas"
},
{
title: "WELCOME TO CANVAS",
paragraph: "Create just what you need for your Perfect Website. Choose from a wide<br>range of Elements & simple put them on your own Canvas"
},
{
title: "WELCOME TO CANVAS",
paragraph: "Create just what you need for your Perfect Website. Choose from a wide<br>range of Elements & simple put them on your own Canvas"
},
{
title: "WELCOME TO CANVAS",
paragraph: "Create just what you need for your Perfect Website. Choose from a wide<br>range of Elements & simple put them on your own Canvas"
}
]
}
}
}
帮帮我。
原文由 Nitish Kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议
上面是一个片段,展示了您的问题的解决方案。这是它的概要:
– https://v2.vuejs.org/v2/guide/list.html#Basic-Usage
v-for
指令有第二个参数给你项目的索引。由于您想要第一项上的
active
类,您可以使用表达式测试index
是否为0
并将其绑定到类属性。