简体   繁体   English

有条件地将 class 应用于列表中的特定项目 Vue.js 3.0

[英]Apply class conditionally to specific item in a list Vue.js 3.0

I'd like to disable specific link in automatically rendered list in Vue.js conditionally, here is what I have at the moment:我想有条件地禁用 Vue.js 中自动呈现列表中的特定链接,这是我目前拥有的:

<ul>
  <li class="nav-item" :key="id" v-for="(item, id) in listOfItems">
    <a class="nav-link" :id="item.id">{{ item.name }}</a>
  </li>
</ul>

Basically in some conditions I'd like to disable one of the links from the listOfItems and get it back active if this condition is not applicable anymore.基本上在某些情况下,如果此条件不再适用,我想禁用listOfItems中的一个链接并使其恢复活动状态。 If I bind conditional class it applies to each item in the list:如果我绑定条件 class 它适用于列表中的每个项目:

:class="[someCondition = 'something'? 'disabled': '']"

How to specify in this condition which item exactly should be disabled if the condition is true?如果条件为真,如何在这种情况下指定应该禁用哪个项目?

You could put an additional property on any items you want checked that way:您可以在要检查的任何项目上添加一个附加属性:

listOfItems: [
  { id: 1, name: 'name1', checkme: true },
  { id: 2, name: 'name2', checkme: false },
]

Then, using object binding syntax and === since you're doing a comparison:然后,使用 object 绑定语法和===因为您正在进行比较:

:class="{ disabled: item.checkme && someCondition === 'something' }"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM