如何在Vue中使用验证

时间:2020-04-18 06:56:58

标签: vue.js vee-validate

我在表单中使用了validate指令来验证内容。

<input type="text" v-model="address" name="address" ref="address" v-validate="{ regex:/^([0-9]+|[0-9]+,[0-9]{0,2}?)$/ }" data-vv-validate-on="blur">

但是它返回如下错误:

vue.runtime.esm.js?2b0e:619 [Vue warn]: Failed to resolve directive: validate

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

docs中所述:

我们可以使用Vue.use()全局方法来使用VeeValidate插件,例如:

import Vue from 'vue';
import VeeValidate from 'vee-validate';

Vue.use(VeeValidate);

或直接包含以下脚本:

<script src="path/to/vue.js"></script>
<script src="path/to/vee-validate.js"></script>
<script>
    Vue.use(VeeValidate); // good to go.
</script>

演示:

Vue.use(VeeValidate);
new Vue({
  el: "#app",
  data: {
    address: ''
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<script src="https://unpkg.com/vee-validate@<3.0.0"></script>
<div id="app">
  <form>
    <span>Enter some text, then remove focus from textbox to validate</span><br/>
    <input type="text" v-model="address" name="address" ref="address" v-validate="{ regex:/^([0-9]+|[0-9]+,[0-9]{0,2}?)$/ }" data-vv-validate-on="blur">
    
    <br/>
    <span>{{ errors.first('address') }}</span>
  </form>
</div>