[英]Vuelidate TypeError when using with Vue 3
I'm trying to use Vuelidate with Vue 3. When I try and add some validators to a component I get the following error:我正在尝试将 Vuelidate 与 Vue 3 一起使用。当我尝试向组件添加一些验证器时,出现以下错误:
index.js?1dce:614 Uncaught (in promise) TypeError: Cannot read property 'super' of undefined
I believe this is to do with the way I am using Vuelidate with the Vue instance.我相信这与我将 Vuelidate 与 Vue 实例一起使用的方式有关。 Here is my main.js file:
这是我的 main.js 文件:
import { createApp } from 'vue';
import App from './App.vue';
import Vuelidate from 'vuelidate';
const app = createApp(App);
app.use(Vuelidate);
app.mount('#app');
I will also include the component code:我还将包括组件代码:
<template>
<div id="signup">
<div class="signup-form">
<form>
<div class="input">
<label for="email">Mail</label>
<input
type="email"
id="email"
@input="$v.email.$touch()"
v-model="email">
<div>{{ $v }}</div>
</div>
</form>
</div>
</div>
</template>
<script>
import { required, email } from 'vuelidate/lib/validators'
export default {
data () {
return {
email: '',
}
},
validations: {
email: {
required: required,
email: email
}
}
}
}
</script>
Does anyone know how to fix this?有谁知道如何解决这一问题? If Vue 3 is not compatible with Vuelidate, can anyone recommend and alternative that is?
如果 Vue 3 与 Vuelidate 不兼容,任何人都可以推荐和替代吗?
Thanks谢谢
From your example, it looks like you are using Vuelidate for Vue 2. Vuelidate for Vue 3 is in alpha (as at the time of writing 30 Sep 2020) but you can still use it.从您的示例来看,您似乎将 Vuelidate 用于 Vue 2。Vue 3 的 Vuelidate 处于 alpha 阶段(截至 2020 年 9 月 30 日撰写本文时),但您仍然可以使用它。 Just install the following libraries.
只需安装以下库。
npm install @vuelidate/core @vuelidate/validators
# or
yarn add @vuelidate/core @vuelidate/validators
You can still use the Options API approach that you are using in your example or move to the Composition API approach.您仍然可以使用您在示例中使用的 Options API 方法,或者转向 Composition API 方法。
import { ref } from 'vue'
import { useVuelidate } from '@vuelidate/core'
import { required, email } from '@vuelidate/validators'
export default {
setup () {
const email = ref('')
const rules = {
email: { required, email }
}
const $v = useVuelidate(rules, { email })
return { email, $v }
}
}
I haven't tried it myself but I hope this helps you.我自己还没有尝试过,但我希望这对你有帮助。
Until it hits production-ready, you can find more information from the following links:在它进入生产就绪状态之前,您可以从以下链接中找到更多信息:
EDIT: I forgot to mention that if you continue to use the Options API approach you need to change your import statement in main.js
to the following:编辑:我忘了提到,如果您继续使用 Options API 方法,您需要将
main.js
中的 import 语句main.js
为以下内容:
import { createApp } from 'vue'
import App from './App.vue'
import { VuelidatePlugin } from '@vuelidate/core'
const app = createApp(App)
app.use(VuelidatePlugin)
app.mount('#app')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.