【VUE】使用elementUI制作获取验证码组件

使用elementUI-【表单验证】的表单,在此基础上进行修改,达到自己需要的样式

eleUI原版代码如下 自行修改需要的样式

<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="活动名称" prop="name">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
  <el-form-item label="活动区域" prop="region">
    <el-select v-model="ruleForm.region" placeholder="请选择活动区域">
      <el-option label="区域一" value="shanghai"></el-option>
      <el-option label="区域二" value="beijing"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="活动时间" required>
    <el-col :span="11">
      <el-form-item prop="date1">
        <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
      </el-form-item>
    </el-col>
    <el-col class="line" :span="2">-</el-col>
    <el-col :span="11">
      <el-form-item prop="date2">
        <el-time-picker placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
      </el-form-item>
    </el-col>
  </el-form-item>
  <el-form-item label="即时配送" prop="delivery">
    <el-switch v-model="ruleForm.delivery"></el-switch>
  </el-form-item>
  <el-form-item label="活动性质" prop="type">
    <el-checkbox-group v-model="ruleForm.type">
      <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
      <el-checkbox label="地推活动" name="type"></el-checkbox>
      <el-checkbox label="线下主题活动" name="type"></el-checkbox>
      <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
    </el-checkbox-group>
  </el-form-item>
  <el-form-item label="特殊资源" prop="resource">
    <el-radio-group v-model="ruleForm.resource">
      <el-radio label="线上品牌商赞助"></el-radio>
      <el-radio label="线下场地免费"></el-radio>
    </el-radio-group>
  </el-form-item>
  <el-form-item label="活动形式" prop="desc">
    <el-input type="textarea" v-model="ruleForm.desc"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
<script>
  export default {
    data() {
      return {
        ruleForm: {
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        },
        rules: {
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ],
          date1: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          date2: [
            { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
          ],
          type: [
            { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
          ],
          resource: [
            { required: true, message: '请选择活动资源', trigger: 'change' }
          ],
          desc: [
            { required: true, message: '请填写活动形式', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

我需要的样式
添加了验证规则的有
-公司名称
-姓名
-手机号
-手机验证码

点击【获取验证码】后 按钮自动显示为倒计时 从60s开始
倒计时状态下无法点击
【立即申请】按钮需要上面必填项均填写后才能点击

代码如下

html

<template>
  <div id="apply">
    <div class="textbox">
      <h1>演示文字演示文字</h1>
      <h2>演示文字</h2>
      <h3>
        请留下您的信息,这是一段演示文字,为这是一段演示文字,这是一段演示文字,这是一段演示文字,这是一段演示文字,这是一段演示文字,这是一段演示文字,。
      </h3>
    </div>

    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      class="demo-ruleForm"
      hide-required-asterisk:true
      inline-message:true
    >
      <el-form-item prop="comp">
        <el-input v-model="ruleForm.comp" placeholder="公司名称"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="ruleForm.name" placeholder="姓名"></el-input>
      </el-form-item>

      <!-- 验证码 -->
      <el-form-item prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item prop="verify">
        <div class="bind_code">
          <el-input
            class="bind_code_input code"
            v-model="ruleForm.verify"
            type="text"
            placeholder="手机验证码"
          />
          <el-button
            @click.native.prevent="bindforgetSendCode"
            class="codebtn"
            :disabled="disabled"
            >{{ btnText }}</el-button
          >
        </div>
      </el-form-item>
      <!-- 验证码完毕 -->

      <el-form-item>
        <el-input
          v-model="ruleForm.textarea"
          class="msg"
          type="textarea"
          maxlength="200"
          resize="none"
          show-word-limit
          placeholder="请您对需求进行简述"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          class="applybtn"
          type="primary"
          @click="submitForm('ruleForm')"
          >立即申请</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

script部分

<script>
import {captchaSms,application} from "@/api/getapi";
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        comp: "",
        phone: "",
        verify: "",
        textarea: "",
        delivery: false,
      },
      rules: {
        comp: [
          { required: true, message: " ", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: " ",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "  ", trigger: "blur" },
          { min: 2, max: 20, message: "  ", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "  ", trigger: "blur" },
          { min: 11, max: 11, message: "  ", trigger: "blur" },
        ],
        verify: [
          { required: true, message: "  ", trigger: "blur" },
          { min: 4, max: 4, message: "  ", trigger: "blur" },
        ],
      },
      //   验证码按钮
      type: "",
      btnText: "获取验证码",
      disabled: false,
    };
  },
  methods: {
    // 发送验证码
    bindforgetSendCode() {
      this.$refs["ruleForm"].validateField("phone", (errorMessage) => {
        if (!errorMessage) {
          captchaSms({phonenumber: this.ruleForm.phone}).then(res => {
            console.log(res);
          })
          this.disabled = true;
          this.btnText = "请稍候...";
          setTimeout(() => {
            this.doLoop(60);
          }, 500);
        } else {
          return false;
        }
      });
    },
    // 手机验证码的倒计时
    doLoop: function (seconds) {
      seconds = seconds ? seconds : 60;
      this.btnText = seconds + "s后获取";
      let countdown = setInterval(() => {
        if (seconds > 0) {
          this.btnText = seconds + "s后获取";
          --seconds;
        } else {
          this.btnText = "获取验证码";
          this.disabled = false;
          clearInterval(countdown);
        }
      }, 1000);
    },

    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.ruleForm));
          delete data.delivery
          application(data).then(res => {
            console.log(res);
          alert("提交成功!");
        } else {
          console.log("提交失败!!");
          return false;
        }
      });
    },
  },
};
</script>

css部分

<style scoped>
body {
  background-color: #347aea;
}
#apply {
  background-color: #347aea;
  width: 375px;
  height: 6.67%;
  position: relative;
}
.textbox {
  width: 375px;
  height: 170px;
  background-color: #3e55da;
  margin-bottom: 55.5px;
}
.textbox h1 {
  font-size: 35px;
  color: #fdfaf9;
  text-align: center;
  line-height: 35px;
  padding: 55.5px 47px 22.5px 47px;
  text-shadow: 0 5px 10px #0e5bd6;
  font-family: SourceHanSansCN-Medium-, SourceHanSansCN-Medium;
}
.textbox h2 {
  font-size: 24.5px;
  color: #fdfaf9;
  text-align: center;
  line-height: 24.5px;
  font-family: SourceHanSansCN-Medium-, SourceHanSansCN-Medium;
  text-shadow: 0 5px 10px #0e5bd6;
}
.textbox h3 {
  font-size: 12px;
  font-family: Source Han Sans CN-Normal, Source Han Sans CN;
  text-align: center;
  color: #fdfaf9;
  font-weight: 400;
  margin: 18.5px 27px 26.5px 27px;
}
.demo-ruleForm {
  width: 343px;
  height: 435px;
  margin: 0px 17px;
  padding-top: 35px;
  padding-bottom: 76px;
  border-radius: 5px;
  background-color: white;
}
.el-form-item {
  margin-bottom: 0px;
}
.el-input {
  width: 279.5px;
  height: 35px;
  margin: 0px 30.5px 11.5px 33px;
  font-size: 11px;
  font-family: PingFang SC-Medium, PingFang SC;
  color: #c0c4cc;
  font-weight: 500;
  border-radius: 2px;
}
.el-button {
  border: 0;
  background: none;
  padding: 0;
  border-radius: 0;
}
.codebtn {
  position: absolute;
  top: 15px;
  right: -15px;
  font-size: 11px;
  font-weight: 500;
  font-family: PingFang SC-Medium, PingFang SC;
  color: #1b3dd1;
  line-height: 15px;
  cursor: pointer;
  padding-left: 10px;
  border-left: 1px solid #1b3dd1;
}
.bind_code {
  width: 279.5px;
  height: 35px;
  position: relative;
}
.msg {
  width: 279.5px;
  height: 70px;
  font-size: 11px;
  font-family: PingFang SC-Medium, PingFang SC;
  color: #c0c4cc;
  font-weight: 500;
  margin: 11.5px 30.5px 0px 33px;
}
.applybtn {
  width: 279px;
  height: 44px;
  color: white;
  font-weight: 400;
  line-height: 44px;
  background: linear-gradient(270deg, #37adff 0%, #1366ff 53%, #37adff 100%);
  border-radius: 2px;
  margin: 0 30.5px 11.5px 33px;
}
</style>
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue可以使用ElementUI的树形组件来展示树形结构的数据。使用步骤如下: 1. 安装ElementUI组件库 可以通过npm或yarn安装ElementUI组件库,具体命令如下: ``` npm install element-ui --save ``` 或 ``` yarn add element-ui ``` 2. 引入ElementUI组件Vue项目的入口文件中,引入ElementUI组件库和样式文件,具体代码如下: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 使用树形组件Vue组件中,使用ElementUI的el-tree组件来展示树形结构的数据,具体代码如下: ```html <template> <el-tree :data="treeData" :props="treeProps"></el-tree> </template> <script> export default { data() { return { treeData: [ { label: '一级 1', children: [ { label: '二级 1-1', children: [ { label: '三级 1-1-1' }, { label: '三级 1-1-2' } ] }, { label: '二级 1-2', children: [ { label: '三级 1-2-1' }, { label: '三级 1-2-2' } ] } ] }, { label: '一级 2', children: [ { label: '二级 2-1', children: [ { label: '三级 2-1-1' }, { label: '三级 2-1-2' } ] }, { label: '二级 2-2', children: [ { label: '三级 2-2-1' }, { label: '三级 2-2-2' } ] } ] } ], treeProps: { label: 'label', children: 'children' } } } } </script> ``` 其中,treeData是树形数据,treeProps是树形组件的配置项,用于指定树形数据中的label和children字段。 以上就是使用ElementUI树形组件的基本步骤。 ### 回答2: Vue是一种流行的JavaScript框架,用于构建用户界面。ElementUI是一个基于Vue的UI组件库,其中包含许多常见的UI组件,包括树形组件ElementUI的树形组件使开发人员能够轻松创建树形结构的界面元素,并可以满足各种需求。 要使用ElementUI的树形组件,首先需要在Vue应用程序中安装ElementUI。有多种方法可以安装ElementUI,其中包括使用npm、CDN或下载源代码。在安装完成后,开发人员可以在需要使用树形组件的地方包含ElementUI中的<el-tree>标记。 使用ElementUI的树形组件非常灵活。开发人员可以通过简单的属性更改来自定义树形组件的行为和外观。例如,可以使用“expand-on-click-node”属性来控制树形节点是否可以通过单击展开/关闭。开发人员还可以使用“:data”属性来传递树形节点的数据。此外,ElementUI还提供了许多事件和插槽,以便更好地控制树形组件的行为。 在许多应用程序中,树形结构是重要的用户界面元素。使用ElementUI的树形组件可以使开发人员更轻松地创建和管理树形结构,并可以提高应用程序的可维护性和性能。无论是开发单页面应用程序还是多页面应用程序,使用ElementUI的树形组件都是一个不错的选择。 ### 回答3: Vue 使用 ElementUI 树形组件非常方便。ElementUI 是一个基于 Vue.js 的组件库,提供了丰富的 UI 组件,其中包括了树形组件。以下是关于 Vue 使用 ElementUI 树形组件的一些注意事项和步骤: 安装 ElementUI: 在使用 ElementUI 之前,需要先安装它。在命令行中执行以下命令即可: ``` npm install element-ui --save ``` 使用 ElementUI 树形组件: 在 Vue 项目中使用 ElementUI 树形组件,需要先引入 ElementUI 和相应的组件。在 main.js 中添加如下内容: ``` import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 接下来就可以在需要使用树形组件的地方添加以下代码: ``` <template> <el-tree :data="data" :props="defaultProps"> </el-tree> </template> <script> export default { data() { return { data: [{ label: '一级 1', children: [{ label: '二级 1-1', children: [{ label: '三级 1-1-1' }] }] }, { label: '一级 2', children: [{ label: '二级 2-1', children: [{ label: '三级 2-1-1' }] }, { label: '二级 2-2', children: [{ label: '三级 2-2-1' }] }] }, { label: '一级 3', children: [{ label: '二级 3-1', children: [{ label: '三级 3-1-1' }] }, { label: '二级 3-2', children: [{ label: '三级 3-2-1' }] }] }], defaultProps: { children: 'children', label: 'label' } } } } </script> ``` 在这个例子中,我们使用了一个简单的树形结构。需要注意的是,在 data 中定义了树的结构,而在 defaultProps 中,则定义了树的属性。 以上就是 Vue 使用 ElementUI 树形组件的简单介绍。使用 ElementUI 提供的树形组件,可以更加轻松地实现树形结构的展示,并且具有良好的用户交互体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值