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

Nit*_*mar 15 javascript vue.js vuejs2

我正在努力学习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>
Run Code Online (Sandbox Code Playgroud)

所以第一个元素应该是这样的:

<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>
Run Code Online (Sandbox Code Playgroud)

我能够获取数据,所以一切都运行得很好.

以下是我的脚本代码:

<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"
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

帮帮我.

Win*_*ing 32

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');
Run Code Online (Sandbox Code Playgroud)
.active {
  background-color: red;
}
Run Code Online (Sandbox Code Playgroud)
<!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>
Run Code Online (Sandbox Code Playgroud)

以上是演示解决问题的片段.这是一个概述:

在内部v-for块中,我们可以完全访问父作用域属性.v-for还支持当前项的索引的可选第二个参数.

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

v-for指令有第二个参数,为您提供项目的索引.

v-for="(item, index) in items"
Run Code Online (Sandbox Code Playgroud)

由于您希望active第一个项目上的类,您可以使用表达式测试,如果index是,0并将其绑定到类属性.

:class="{ 'active': index === 0 }"
Run Code Online (Sandbox Code Playgroud)

  • @Glen:假设你的_index_是一个字符串,我假设你正在迭代一个对象,而不是一个数组.当用`v-for`迭代一个对象时,`index`参数位于对象"`中的第三个位置(`v-for ="(value,key,index).你可以在绑定到的表达式中使用该参数如果该项是第一个,则测试类属性.但请注意:**在迭代对象时,顺序基于`Object.keys()`的键枚举顺序,不保证一致JavaScript引擎实现.** (3认同)

Bra*_*ane 6

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

:class="{ 'active': index === 0 }"
Run Code Online (Sandbox Code Playgroud)

这是制作的工作示例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>
Run Code Online (Sandbox Code Playgroud)

另外,如果您有多个课程,可以像这样混合使用:

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