如何将class="active"放入vuejs for循环中的第一个元素

How to put class="active" to first element in vuejs for loop

我正在努力学习 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"
                }
            ]
        }
    }
}

帮帮我。

const TextComponent = {
  template: `
    <p>{{ text }}</p>
  `,
  
  props: ['text'],
};

new Vue({
  components: {
    TextComponent,
  },
  
  template: `
    <div>
      <text-component
        v-for="(item, index) in items"
        :class="{ 'active': index === 0 }"
        :text="item.text">
      </text-component>
    </div>
  `,
  
  data: {
    items: [
      { text: 'Foo' },
      { text: 'Bar' },
      { text: 'Baz' },
    ],
  },
}).$mount('#app');
.active {
  background-color: red;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title></title>
</head>
<body>
  <div id="app"></div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
</body>
</html>

以上是演示问题解决方案的片段。这是它的概要:

Inside v-for blocks we have full access to parent scope properties. v-for also supports an optional second argument for the index of the current item.

https://vuejs.org/v2/guide/list.html#Basic-Usage

v-for 指令有第二个参数给你项目的索引。

v-for="(item, index) in items"

因为你想要 active class 在第一个项目上,你可以使用表达式测试 index 是否为 0 并将其绑定到 class 属性.

:class="{ 'active': index === 0 }"

最简单的解决方案是检查每个元素是否 index 等于 0,然后设置 active class(或您需要的 class)。这是 class 定义的代码:

:class="{ 'active': index === 0 }"

这是制作 Bootstrap Tabs:

的工作示例
<ul class="nav nav-tabs tab-nav-right" role="tablist">
    <li role="presentation" :class="{ 'active': index === 0 }" v-for="(value, index) in some_array" v-bind:key="index">
        {{some_array[index]}}
    </li>
</ul>

此外,如果您有多个 classes,您可以像这样混合它们:

:class="['tab-pane fade', (index === 0 ? 'active in' : 'something_else')]"

要将活动 class 放入循环中的第一个元素,基本上,您正在尝试通过 VuejS 以编程方式控制 class。

VueJS 允许你将锚标签的 class 直接绑定到 li 元素的索引上,这样当 vuejs 变量绑定到索引上时变化时,class 也会变化。查看这两个链接了解更多详情

这是解决的关键

:class="{current:i == current}

在下面的 fiddle 和另一个 post 上可用,它以博客格式解释了如何在 vuejs 中动态控制 li 元素的 class

https://jsfiddle.net/Herteby/kpkcfcdw/

https://stackoverblow.wordpress.com/2021/04/03/how-modern-javascript-makes-click-simulation/