使用 Vuelidate,您可以使用this.$v.$reset()
. 在此Codepen 示例中,重置lastName
使用 Vuetify 组件的字段有效 -设置为 false$invalid
时$error
为 true。
重置常规文本输入时firstName
它不起作用,因为$error
标志仍然为真。如何修改文本输入,以便在调用重置时 $error 为假?
我也试过this.$nextTick(() => {...})
,但这也不起作用。
Vue.use(window.vuelidate.default)
var validationMixin = window.vuelidate.validationMixin
const {
maxLength,
required
} = window.validators
new Vue({
el: '#app',
mixins: [validationMixin],
data: () => ({
form: {
firstName: '',
lastName: ''
}
}),
validations: {
form: {
firstName: {
required, maxLength: maxLength(2)
},
lastName: {
required, maxLength: maxLength(2)
},
}
}
})
input.raw {
border: solid;
}
.is-invalid {
border-color: #FF5252 !important;
}
<html>
<head>
<script src="https://unpkg.com/vuelidate@0.6.1/dist/validators.min.js"></script>
<script src="https://unpkg.com/vuelidate@0.6.1/dist/vuelidate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
</head>
<body>
<div id="app">
<label for="firstName">First Name</label>
<input
v-model="form.firstName"
id="firstName"
class="raw"
:class="{ 'is-invalid': $v.form.firstName.$error }"
type="text"
width="100%"
:oninput="$v.form.firstName.$touch()"
:onblur="$v.form.firstName.$touch()"
/>
<button @click="$v.form.firstName.$touch()">
$touch
</button>
<button @click="$v.form.firstName.$reset()">
$reset
</button>
<pre>{{ $v.form.firstName }}</pre>
</div>
</body>
</html>