javascript - 更新状态时Vue类绑定不起作用
问题描述
我正在尝试根据 v-for 中的索引进行 Vue 类绑定。我可以看到 Vue 开发工具中的状态正确更改,但类名没有更新。
<div class="group" v-for="(role, index) in roles_be">
<span class="group-name"> {{ role.name }}</span>
<i class="fas fa-info-circle"></i>
<i class="fa fa-fw switch-icon" @click="toggleGroupElements(index)" v-bind:class="[
groupElements.index ? 'fa-toggle-on' : 'fa-toggle-off' ]"></i>
</div>
groupElements: [false, false, false, false],
toggleGroupElements(index) {
switch (index) {
case 0:
this.groupElements[0] = !this.groupElements[0];
break;
case 1:
this.groupElements[1] = !this.groupElements[1];
break;
case 2:
this.groupElements[2] = !this.groupElements[2];
break;
case 3:
this.groupElements[3] = !this.groupElements[3];
break;
}
},
解决方案
由于您尝试访问数组中的索引而不是对象中的键,因此您应该尝试groupElements[index]
在类绑定中。
:class="[groupElements[index] ? 'fa-toggle-on' : 'fa-toggle-off' ]"
推荐阅读
- python - 使用 Python Multiprocessing 包解析文本文件的问题
- python - 如何使用 instaloader 下载特定数量的照片(不是整个帖子)
- search - 估计压缩整数列表上的最大有效负载大小
- django - 是否可以在 Google App Engine 上部署 django 2.2?
- c++ - 使用指针和向量
- docker - 无法使用 Azure AD 身份验证在 Docker 中运行 .Net 核心应用程序
- c - 这种变量分配是如何工作的?
- django - '|' 是什么意思 在 {{ services|pprint|safe }} 在 django 中是什么意思?
- ios - UIFileSharingEnabled 键应该如何出现在 Info.plist 中
- php - 两次之间的差异导致错误