简体   繁体   English

Vuelidate 与 Vue 3 + vue-class-component + TypeScript

[英]Vuelidate with Vue 3 + vue-class-component + TypeScript

Does anyone know any working example with the mentioned stack?有谁知道上述堆栈的任何工作示例? I know Vuelidate is still alpha when it comes to Vue 3, but my guess is if it works with Composition API, then there should be a workaround to make it work with classes.我知道 Vuelidate 在 Vue 3 中仍然是 alpha 版本,但我的猜测是,如果它适用于 Composition API,那么应该有一种解决方法让它适用于类。

I'm trying the following simple example:我正在尝试以下简单示例:

<template>
  <input
    v-model="v$.login.$model"
    :class="{ wrongInput: v$.login.$errors.lenght }"
    placeholder="Login"
  />
</template>

<script lang="ts">
import { Vue } from 'vue-class-component';
import useVuelidate from '@vuelidate/core';
import { required } from '@vuelidate/validators';

export default class LoginForm extends Vue {
  login = '';

  v$ = useVuelidate();

  validations() {
    return {
      login: {
        required,
      },
    };
  }
}
</script>

And the error is:错误是:

Uncaught (in promise) TypeError: _ctx.v$.login is undefined

The docs says I somehow need to pass rules and state to useVuelidate(), but I can't figure how and whether it is the reason for the error. 文档说我需要以某种方式通过规则和 state 才能使用 Vuelidate(),但我不知道它是如何以及是否是错误的原因。 Any help is appreciated.任何帮助表示赞赏。

In your example, validations is a component method, but Vuelidate expects it as a component option.在您的示例中, validations是一种组件方法,但 Vuelidate 期望它作为组件选项。

The fix is to move validations into @Options :解决方法是将validations移至@Options

import { Vue, Options } from 'vue-class-component';
import useVuelidate from '@vuelidate/core';
import { required } from '@vuelidate/validators'

@Options({
👉validations: {
    login: { required }
  }
})
export default class LoginForm extends Vue {
  login = '';

  v$ = useVuelidate();
}

Tested with @vuelidate/core@2.0.0-alpha.15使用@vuelidate/core@2.0.0-alpha.15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 vue-class-component &amp; Mixins - vue-class-component & Mixins Vue&TypeScript:vue-property-decorator / vue-class-component的&#39;el&#39;属性? - Vue&TypeScript: 'el' property for vue-property-decorator/vue-class-component? vue-class-component + typescript:如何在导入的 function 中使用组件的 class 作为“this”类型? - vue-class-component + typescript: how to use a component's class as a type of "this" in imported function? Vue JS - vue-class-component 绑定输入 - Vue JS - vue-class-component binding input VueJS 组件:具有 vue-class-component 的单独文件的代码覆盖率 - VueJS Component: Code coverage for separate file with vue-class-component vue-class-component:如何创建和初始化反射数据 - vue-class-component : how to create and initialize reflective data vue类组件mixins在WebStorm中引发错误? - vue-class-component mixins provoke an error in WebStorm? Vue 2.5 vue-class-component使用Vue命名空间,但是它是Type - Vue 2.5 vue-class-component uses Vue namespace but it's a Type vue-class-component 语法中 vue-models(因此,“props”)的计算 getter/setter - Computed getter/setter for vue-models (therefore, “props”) in vue-class-component syntax 使用vue-class-component的Vue Router:next函数不接受回调选项 - Vue Router with vue-class-component: next function does not accept callback option
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM