Native Html introduces element UI + vue3 form verification settings

Effect:

When submitting, the inspection results are displayed 

html source code

<!DOCTYPE html>
<html>
<!--带搜索输入框下拉弹窗
-->
<head>
  <meta charset="UTF-8">
  <!-- import Vue before Element -->
  <script src="../js/vue3.3.8/vue.global.js"></script>
  <link rel="stylesheet" href="../js/elementPlus/index.css">
  <script src="../js/elementPlus/index.full.js"></script>
  <title></title>

</head>
<body>
<div id="app">
  <!-- 双大括号语法,可以直接拿到下面data中return 里面的数据 -->

  <el-button type="success" @click="onsubmit">保存按钮</el-button>

  <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm"
           status-icon>
    <div class="section-header">
      商品信息
      <el-checkbox label="自动" size="large" checked></el-checkbox>
    </div>
    <el-row>
      <el-col :span="9">
        <el-form-item label="商品名称" prop="name">
          <el-input placeholder="商品名称长度必须小于20" v-model="formData.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="9">
        <el-form-item label="商品编码" prop="code">
          <el-input placeholder="系统自动生成" v-model='formData.code'></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-divider></el-divider>

  </el-form>

</div>
</body>

<script>
  const {createApp, ref, reactive} = Vue
  const {ElMessageBox} = ElementPlus

  const app = createApp({
    setup() {
      const message = ref('Hello vue!')
      const formData = ref({name: '', code: ''})
      const rules = reactive({
        name: [{required: true, trigger: 'blur'}],
        code: [{required: true, trigger: 'blur'}],
      })
      const onsubmit = (e) => {
        console.log(vm.$refs.ruleFormRef)
        vm.$refs.ruleFormRef.validate(valid => {
          if (valid) {
            alert('校验成功')
          } else {
            alert('失败')
          }
        })
      }

      return {
        message, formData, rules, onsubmit,
        change: (e) => {
          console.log('修改值', e)
        }
      }
    },
    created() {
    },
    mounted() {
      console.log('|--mounted', this)
    }
  })
  app.use(ElementPlus)
  const vm = app.mount('#app')
</script>
</html>

 The form object ruleFormRef is obtained here at the beginning, but cannot be obtained with this

 const vm = app.mount('#app') 

Because of this sentence, it finally hit the mark. Through the instance of vm, we get: vm.$refs.ruleFormRef.validate() is tested. result.

Related:

Nested in vue3 project, import old project jQuery project to reduce repeated development - CSDN Blog 

Guess you like

Origin blog.csdn.net/LlanyW/article/details/134998205