我正在使用 Laravel 和 Vue.js 创建多个用户。我正在创建一组用户并填写将他们保存到表中所需的字段。当所有字段都填满后,我在 vue.js 中循环用户数组并将它们一一发送到服务器端。服务器端验证它们(是用户名和电子邮件的唯一值)。如果发生错误,它会将错误发送回客户端。在客户端,循环停止,然后向用户添加错误数组。
this.users.user.push(response.data);
我想这样显示错误:
<td class="form-group" v-bind:class="{ 'has-error': users[index].errors.includes('username') }">
<input v-model="user.username" type="text" class="form-control full-width">
</td>
问题是,在服务器端进行验证之前,用户没有错误数组。我可以在将空数组添加到用户之前将它插入用户模型,但我想知道是否有其他方法可以实现此目的。
是否有另一个 if 语句可以替换 users[index].errors.includes('username')
来检查是否存在错误,然后检查错误数组是否包含用户名?
最佳答案
你可以做
v-bind:class = "{ 'has-error': users && users[index] && users[index].errors && users[index].errors.includes('username') }"
这将处理 undefined
关于javascript - Vue.js : v-bind:class if an array exist and that array include something,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57418254/