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"

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

相关推荐
放下华子我只抽RuiKe56 小时前
FastAPI 全栈后端(三):数据库与 ORM
前端·数据库·react.js·oracle·性能优化·前端框架·fastapi
源图客6 小时前
境外电商 - 龙虾智能体-综合选品推荐报告
开发语言·javascript·ecmascript
磊 子6 小时前
C++设计模式
javascript·c++·设计模式
梵得儿SHI6 小时前
Vue 项目实战与性能优化全攻略:从代码、渲染到首屏,一站式解决卡顿慢加载
前端·vue.js·性能优化·vite·前端面试·前端优化·首屏优化
ShyanZh6 小时前
【skill】HTML PPT Skill:用 Claude Code 一句话生成专业演示文稿
前端·ai·html·powerpoint·skill
AI视觉网奇6 小时前
three教学 3d资产拼接源代码
前端·css·css3
meilindehuzi_a7 小时前
构建基于 RESTful 架构的 TodoList 全栈应用:从前后端理论到 TypeScript/Bun 实战
架构·typescript·restful
程序猿阿伟7 小时前
《Chrome标签组搭建多任务高效浏览指南》
前端·chrome
2601_958352907 小时前
双麦 DSP 音频模块实战:一文梳理 A-68 在全行业场景的声学解决方案与落地要点
前端·嵌入式硬件·音视频·语音识别·降噪消回音·音频处理模块
智码看视界8 小时前
老梁聊全栈:JavaScript 原型链深入探索对象继承的奥秘
前端·javascript·ecmascript