小编Igo*_*rov的帖子

使用 Vue 3 + vue-class-component + TypeScript 进行 Vuelidate

有谁知道上述堆栈的任何工作示例?我知道 Vuelidate 在 Vue 3 方面仍然是 alpha,但我的猜测是,如果它可以与 Composition API 一起使用,那么应该有一个解决方法可以使其与类一起使用。

我正在尝试以下简单示例:

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

Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

错误是:

Uncaught (in promise) TypeError: _ctx.v$.login is undefined
Run Code Online (Sandbox Code Playgroud)

文档说我需要以某种方式传递规则和状态来 useVuelidate ( ),但我无法弄清楚它是如何以及是否是错误的原因。任何帮助表示赞赏。

typescript vue.js vuelidate vue-class-components

6
推荐指数
1
解决办法
4036
查看次数

Vue 3 使用 v-model 作为 props 而不是 :prop 和 @emit

因此,我已经阅读了这篇文章几次,据我了解,我可以使用 v-model 而不是 props,将值从父组件传递到子组件,并在子组件中修改 prop 的值时自动发出事件,从而用更少的代码获得双向绑定(不需要捕获父级中发出的事件)。然而它并没有按照我认为应该的方式工作。

这是我的代码:

<template>
  <!-- This is ParentComponent.vue -->
  <ChildComponent v-model:documents="user.documents" />
</template>
Run Code Online (Sandbox Code Playgroud)
<script lang="ts">
  // This is ParentComponent.vue
  import { Vue, Options } from 'vue-class-component';
  import UserClass from '@/some/place/UserClass';
  import ChildComponent from '@/components/ChildComponent.vue';

  @Options({
    components: {
      ChildComponent,
    }
  })
  export default class ParentComponent extends Vue {
    // Local state.
    user: UserClass = new UserClass();
  }
</script>
Run Code Online (Sandbox Code Playgroud)
<template>
  <!-- This is ChildComponent.vue -->
  <section v-for="document in documents" :key="document.id">
    {{ document.name }}
    <button @click="remove(document.id)">Delete document</button> …
Run Code Online (Sandbox Code Playgroud)

typescript vue-class-components vuejs3

6
推荐指数
1
解决办法
1万
查看次数

Vue 3 + TypeScript + &lt;script setup&gt; 中的完整 props 声明(带有默认值和验证器)

下面是现代 Vue 3 props 声明的基本示例,其中<script setup lang="ts">包含强制name和可选age

// <script setup lang="ts">
defineProps<{
  name: string,
  age?: number
}>()
// </script>
Run Code Online (Sandbox Code Playgroud)

这是相同的声明,但具有age默认值:

// <script setup lang="ts">
withDefaults(defineProps<{
  name: string,
  age?: number
}>(), {
  age: 20
})
// </script>
Run Code Online (Sandbox Code Playgroud)

我的主要问题是:有没有办法将验证器合并到这个新语法中?或者每当我需要添加验证器时,我需要回滚到旧语法,即:

// <script setup lang="ts">
defineProps({
  age: {
    type: Number,
    validator(value: number) => value > 0
  }
})
// </script>
Run Code Online (Sandbox Code Playgroud)

我发现了类似的主题,其中一些人建议使用泛型类型并传递参数来定义属性,如下所示:

// <script setup lang="ts">
defineProps<{
  age: number
}>({
  age: {
    validator(value: number) => …
Run Code Online (Sandbox Code Playgroud)

validation typescript vuejs3

6
推荐指数
0
解决办法
756
查看次数

C#字段资本化公约

我的教科书(Visual C#How to Program,6/e)规定C#中的字段应该使用camelCase.这与Microsoft C#指南中提供的示例相对应:https: //docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields

public class CalendarEntry
{
    // private field
    private DateTime date;
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是官方的Microsoft命名约定明确指出字段应该使用PascalCase(尽管它们没有提供私有字段的例子,因为它们应该是正常的):https://docs.microsoft.com/en-us/dotnet/standard/ 设计指南/市值公约

标识符:字段,外壳:Pascal,示例:

class MessageQueue
{
    public static readonly TimeSpan InfiniteTimeout;
}

public struct UInt32
{
    public const Min = 0;
}
Run Code Online (Sandbox Code Playgroud)

那么,我怎么知道根据MS编码惯例使用什么案例来保持我的编码风格?

c#

3
推荐指数
1
解决办法
2141
查看次数