使用 Vue 3 进行 Vuelidate vue 类组件 &打字稿

发布于 2025-01-20 13:47:11 字数 2446 浏览 1 评论 0原文

我正在尝试 vludate 带有Vue 3,但我无法得到它可以使用Vue-Class-Component&适合我的典型模式。打字稿语法。我看到这个类似的问题一些破裂的变化,导致我现在遇到的相同问题。谁能帮忙翻译这个?

这个示例有效,这是我要学习转换的方法:

    <template>
      <div>
        <input type="email" placeholder="Email..."  v-model="email" @blur="v$.email.$touch" :class="{error: v$.email.$error}" >

        <div v-if="v$.email.$error">
          <p v-if="v$.email.email.$invalid && email !==''"
          >Invalid Email</p>
          <p v-if="v$.email.required.$invalid"
          >Required</p>
        </div>

        <button type="submit" style="background-color:yellow"
        >Submit</button>
      </div>
    </template>
    <script lang="ts">
    import useVuelidate from "@vuelidate/core";
    import {required, email} from "@vuelidate/validators";
    
    export default {
      setup () {
        return { v$: useVuelidate() }
      },
      data(){ return{ email: null, } },
      validations:{ email:{ required, email } }
    }
    </script>
    <style scoped lang="scss"> .error{ color:red; } </style>

我已经尝试了许多不同的安排,但是这是我最近的[破碎]脚本...直接的问题是,在模板中,> v $ .. email未被识别:属性'电子邮件'不存在于type'ref&lt; lt; lt; lt; validationArgs&lt; unknown&gt;,未知&gt;&gt;'。我认为我没有正确地使用Vuelidate“注册”电子邮件。

    <script lang="ts">
    import { Vue, Options } from "vue-class-component";
    import useVuelidate from "@vuelidate/core";
    import {required, email} from "@vuelidate/validators";
    //import { Validate } from "vuelidate-property-decorators"; 
     @Options({
       validations:{
         email:{required,email}
       }
     })
    export default class ValidationsAsClass extends Vue {
      v$ = useVuelidate();
      
      //Use of decorator would be best...
      //@Validate({required,email})
      email = "";
     
    }
    </script>

任何帮助将不胜感激。任何有关将标准VUE转换为类分组语法的信息的信息也将不胜感激。

I'm trying out Vulidate with Vue 3, but I can't get it to fit into my typical pattern using the vue-class-component & Typescript syntax. I see this similar question, but I expect there have been some breaking changes that cause the same issues I am having, now. Can anyone help translate this?

This example works, and it's what I'm trying to learn to convert:

    <template>
      <div>
        <input type="email" placeholder="Email..."  v-model="email" @blur="v$.email.$touch" :class="{error: v$.email.$error}" >

        <div v-if="v$.email.$error">
          <p v-if="v$.email.email.$invalid && email !==''"
          >Invalid Email</p>
          <p v-if="v$.email.required.$invalid"
          >Required</p>
        </div>

        <button type="submit" style="background-color:yellow"
        >Submit</button>
      </div>
    </template>
    <script lang="ts">
    import useVuelidate from "@vuelidate/core";
    import {required, email} from "@vuelidate/validators";
    
    export default {
      setup () {
        return { v$: useVuelidate() }
      },
      data(){ return{ email: null, } },
      validations:{ email:{ required, email } }
    }
    </script>
    <style scoped lang="scss"> .error{ color:red; } </style>

I've tried a lot of different arrangements, but here's my most recent [broken] script... The immediate problem is, in the template, v$.email is not recognized: Property 'email' does not exist on type 'Ref<Validation<ValidationArgs<unknown>, unknown>>'.ts(2339) Which makes me think I'm not "registering" email with Vuelidate properly.

    <script lang="ts">
    import { Vue, Options } from "vue-class-component";
    import useVuelidate from "@vuelidate/core";
    import {required, email} from "@vuelidate/validators";
    //import { Validate } from "vuelidate-property-decorators"; 
     @Options({
       validations:{
         email:{required,email}
       }
     })
    export default class ValidationsAsClass extends Vue {
      v$ = useVuelidate();
      
      //Use of decorator would be best...
      //@Validate({required,email})
      email = "";
     
    }
    </script>

Any help is greatly appreciated. Any source to learn more about translating standard Vue to class-component syntax would be greatly appreciated, as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文