最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue.js how to add active class if index is 0 - Stack Overflow

programmeradmin0浏览0评论

this is my code

<li v-for="(data, index) in datas" :key="data.id" class="nav-item">
  <a :href="#" :class="'nav-link '+ { 'active' : index === 0 }"  data-toggle="tab">
    {{data.name}}
  </a>
</li>

but output only showing like this

<a href="#" data-toggle="tab" class="nav-link [object Object]">test</a>

this is my code

<li v-for="(data, index) in datas" :key="data.id" class="nav-item">
  <a :href="#" :class="'nav-link '+ { 'active' : index === 0 }"  data-toggle="tab">
    {{data.name}}
  </a>
</li>

but output only showing like this

<a href="#" data-toggle="tab" class="nav-link [object Object]">test</a>
Share Improve this question edited Aug 22, 2019 at 19:24 Boussadjra Brahim 1 asked Feb 7, 2019 at 22:26 Job RajanJob Rajan 1951 gold badge6 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You should do it as follows by separating the bound class from the not bound one :

<a :href="#" class="nav-link" :class="{ 'active' : index === 0 }"  data-toggle="tab">
  {{data.name}}
</a>

You can add static class with dynamic class bindings by taking array of classes instead of concatenating by '+' operator.

<li v-for="(data, index) in datas" :key="data.id" class="nav-item">
  <a :href="#" :class="[nav-link,{ 'active' : index === 0 }]"  data-toggle="tab">
    {{data.name}}
  </a>
</li>
发布评论

评论列表(0)

  1. 暂无评论