class - 在 Vue.js 中连接多个类
问题描述
我想用更少的代码编写一个函数,该函数将添加活动类名并自动删除所有其他活动类名。但就我而言,JavaScript 还需要一个唯一的类名。但是想把这一切都放在类名中。我怎样才能使它成为一个有效的类名。有没有办法做到这一点,所以它不会相互冲突。
<ul class="three">
<li
v-for="(post, index) in listData.data"
:key="index"
:class="'list-item unordered-list ' + post.name.toLowerCase() + { active : activeName == post.name}"
@click="showInfo(post.name, post.description)">
{{ post.name }}
</li>
</ul>
解决方案
查看类绑定的对象语法或数组语法,它允许绑定到由值返回的对象或数组。这样,您可以通过从模板调用函数来简化复杂的类或样式组合,例如文档中的示例:
<div v-bind:class="classObject"></div>
...
data: {
isActive: true,
error: null
},
computed: {
classObject: function () {
return {
active: this.isActive && !this.error,
'text-danger': this.error && this.error.type === 'fatal'
}
}
}
推荐阅读
- php - 在下拉框中选择一个选项并在 php 中显示其相关数据?
- python - strip() 正则表达式替换说明
- java - 如何在 put 方法中只发送一个参数?
- java - 如何在 Java 中订阅新的套接字消息
- symfony - Symfony,如何将字段添加到不在表单上的数据库
- c# - 如何将答案选择保存到访问数据库c#
- vector - 如何在迭代期间改变向量的项目?
- java-io - 如果我们将资源作为参数传递,我们可以对资源使用 try 吗,是否会在调用的方法完成执行之前关闭流
- xml - Play Xml 解析器不处理 BOM(字节排序标记)
- php - 如何打印或回显数组的值