vue3+antv+ts实现勾选同意协议复选框之后才能继续注册登录

效果如下:

勾选复选框之前

勾选复选框之后

这里偷懒了,没有把登录和注册按钮分开控制,自己实操的时候可以去细化一下功能

代码如下:

javascript 复制代码
<script setup lang="ts">
import { ref, defineProps, reactive } from "vue";
import { login, register } from "@/api";
import { useRouter } from "vue-router";
import { setToken } from "@/utils/auth";

const router = useRouter();
const formRef = ref<any>(); // Form reference can be of any type
const { loginName, userName, password } = defineProps({
  loginName: String,
  userName: String,
  password: String,
});
const agree = ref<any>(false);
const formState = reactive({
  loginName: "Saucesy",
  userName: "saucesy",
  password: "123456",
});
const formRules = reactive({
  loginName: [{ required: true, message: "请输入账号" }] as const,
  userName: [{ required: true, message: "请输入昵称" }] as const,
  password: [{ required: true, message: "请输入密码" }] as const,
});

enum SubmitType {
  LOGIN = 1,
  REGISTER = 2,
}

const onSubmit = (type: SubmitType): void => {
  formRef.value?.validate().then((value: any) => {
    if (type === SubmitType.LOGIN) {
      login(value).then(onFulfilled);
    } else {
      register(value).then(onFulfilled);
    }
  });

  function onFulfilled(token: string): void {
    console.log(token);
    router.push({ path: "/", replace: true });
  }
};
</script>
<template>
  <div class="page">
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      @finish="onSubmit"
    >
      <a-form-item label="账号" name="loginName">
        <a-input v-model:value="formState.loginName">
          <template #prefix>
            <UserOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item label="昵称" name="userName">
        <a-input v-model:value="formState.userName">
          <template #prefix>
            <UserOutlined class="site-form-item-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item label="密码" name="password">
        <a-input-password v-model:value="formState.password">
          <template #prefix>
            <LockOutlined class="site-form-item-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item>
        <a-checkbox v-model:checked="agree">我已阅读并同意用户守则</a-checkbox>
      </a-form-item>

      <a-form-item>
        <div style="display: flex; gap: 20px">
          <a-button
            style="flex: 3"
            type="primary"
            @click="onSubmit(submitType.LOGIN)"
            :disabled="!agree"
          >
            登录</a-button
          >
          <a-button
            style="flex: 1"
            type="primary"
            @click="onSubmit(submitType.REGISTER)"
            :disabled="!agree"
          >
            注册</a-button
          >
        </div>
      </a-form-item>
    </a-form>
  </div>
</template>

<style scoped lang="scss">
.page {
  margin: 100px auto;
  width: 400px;
}
</style>

关键的地方就三个:

javascript 复制代码
const agree = ref<any>(false);
javascript 复制代码
      <a-form-item>
        <a-checkbox v-model:checked="agree">我已阅读并同意用户守则</a-checkbox>
      </a-form-item>

和这里的:disabled="!agree"

复选框后要再加上需要同意的条款,做一个弹出框或者其他什么都可以

相关推荐
weixin_443290699 分钟前
【阅读记录-章节2】Infrastructure as Code Dynamic Systems for the Cloud Age
运维·前端
山河木马10 分钟前
前端学C++可太简单了:指针
前端·javascript·c++
EndingCoder11 分钟前
算法在前端框架中的集成
前端·javascript·算法·前端框架·排序算法
知识分享小能手26 分钟前
Vue3 学习教程,从入门到精通,Vue 3 表单控件绑定详解与案例(7)
前端·javascript·vue.js·学习·前端框架·vue3·anti-design-vue
三天不学习31 分钟前
CSS :root伪类详解:实现动态主题切换的关键所在
前端·css·root·主题换肤·css 伪类
江城开朗的豌豆34 分钟前
Vue图片懒加载:极简方案 vs 兼容全攻略
前端·javascript·vue.js
oil欧哟37 分钟前
🧐 AI 批量检查数千份技术文档,如何实现高效文档纠错?
前端·人工智能·ai编程
江城开朗的豌豆39 分钟前
Vue组件data必须用函数?这个设计暗藏玄机!
前端·javascript·vue.js
前端小巷子1 小时前
web域名解析
前端·javascript·面试
LaoZhangAI1 小时前
沉浸式翻译API深度解析:500万用户的翻译神器如何配置[2025完整指南]
前端·后端