【问题标题】:vue2 forms using composition apivue2 表单使用组合 api
【发布时间】:2021-09-11 11:48:36
【问题描述】:

我是 Vue 新手,在尝试使用组合 API 创建表单时遇到了问题。 这是我的代码:

import { defineComponent, ref } from "@vue/composition-api";

export default defineComponent({
  name: "Register",
  setup() {
    const form = ref(null);
    const submitted = ref(false);
    const valid = ref(false);
    const checkbox = ref(false);
    const email = ref(null);
    const emailRules = [
      (v) => !!v || "E-mail is required",
      (v) => /.+@.+/.test(v) || "E-mail must be valid",
    ];

    const submit = () => {
      submitted.value = true;
      console.log(form.value.validate());
    };

    return { form, checkbox, email, emailRules, valid, submitted, submit };
  },
});

我的模板如下所示:

<template>
  <base-login-page>
    <v-tabs>
      <v-tab>Sign up</v-tab>
      <v-tab>Sign in</v-tab>

      <v-tab-item class="section">
        <v-form ref="form" v-model="valid" lazy-validation>
          <v-text-field
            v-model="email"
            :rules="emailRules"
            label="E-mail"
            required
            outlined
          ></v-text-field>

          <v-checkbox
            v-model="checkbox"
            :rules="[(v) => !!v || 'You must agree to continue!']"
            label="Do you agree?"
            required
          ></v-checkbox>

          <base-button
            :disabled="!valid || !submitted"
            color="primary"
            class="mr-4"
            @click="submit()"
          >
            Validate
          </base-button>
        </v-form>
      </v-tab-item>
      <v-tab-item>Content for Item Two</v-tab-item>
    </v-tabs>
  </base-login-page>
</template>

<script src="./register.component.ts" lang="ts"></script>
<style src="./register.component.scss" lang="scss" scoped></style>

虽然一切正常,但我在控制台中收到此错误:

[Vue 警告]:避免在运行时向 Vue 实例或其根 $data 添加响应式属性 - 在 data 选项中预先声明它。

我不知道这是什么意思,有人可以帮我吗?

【问题讨论】:

    标签: javascript vuejs2 vuetify.js vue-composition-api


    【解决方案1】:

    尝试在setup() 函数中声明:rules="[(v) =&gt; !!v || 'You must agree to continue!']":disabled="!valid || !submitted" 之类的emailRulesref

    【讨论】:

    • 看起来不太对;我将所有常量更新为 ref ,然后没有 ref 并得到错误:(
    猜你喜欢
    • 2021-09-13
    • 2019-02-28
    • 2021-03-07
    • 1970-01-01
    • 2020-09-10
    • 2022-08-19
    • 2021-03-16
    • 2020-12-19
    • 2020-01-11
    相关资源
    最近更新 更多