当前位置:首页 > VUE

vue 实现新增表单

2026-02-20 01:26:54VUE

实现新增表单的基本步骤

在Vue中实现新增表单功能,可以通过以下方式完成。假设使用Vue 3和Composition API。

模板部分

<template>
  <form @submit.prevent="handleSubmit">
    <div>
      <label for="name">名称:</label>
      <input id="name" v-model="formData.name" type="text" />
    </div>
    <div>
      <label for="email">邮箱:</label>
      <input id="email" v-model="formData.email" type="email" />
    </div>
    <button type="submit">提交</button>
  </form>
</template>

脚本部分

<script setup>
import { reactive } from 'vue'

const formData = reactive({
  name: '',
  email: ''
})

const handleSubmit = () => {
  console.log('提交数据:', formData)
  // 这里可以添加API调用或其他处理逻辑
  // 例如:axios.post('/api/users', formData)
}
</script>

表单验证的实现

使用Vuelidate进行表单验证可以提升用户体验。

安装Vuelidate:

npm install @vuelidate/core @vuelidate/validators

实现验证:

<script setup>
import { reactive } from 'vue'
import useVuelidate from '@vuelidate/core'
import { required, email } from '@vuelidate/validators'

const formData = reactive({
  name: '',
  email: ''
})

const rules = {
  name: { required },
  email: { required, email }
}

const v$ = useVuelidate(rules, formData)

const handleSubmit = async () => {
  const isValid = await v$.value.$validate()
  if (!isValid) return

  console.log('验证通过,提交数据:', formData)
}
</script>

表单重置功能

添加重置按钮可以方便用户重新填写表单。

<template>
  <form @submit.prevent="handleSubmit" @reset="handleReset">
    <!-- 表单字段 -->
    <button type="submit">提交</button>
    <button type="reset">重置</button>
  </form>
</template>

<script setup>
const handleReset = () => {
  formData.name = ''
  formData.email = ''
  v$.value.$reset()
}
</script>

表单加载状态

在提交表单时显示加载状态可以提升用户体验。

<script setup>
import { ref } from 'vue'

const isLoading = ref(false)

const handleSubmit = async () => {
  isLoading.value = true
  try {
    await axios.post('/api/users', formData)
    // 处理成功情况
  } catch (error) {
    // 处理错误
  } finally {
    isLoading.value = false
  }
}
</script>
<button type="submit" :disabled="isLoading">
  {{ isLoading ? '提交中...' : '提交' }}
</button>

表单成功/错误反馈

添加成功和错误提示可以让用户明确操作结果。

vue 实现新增表单

<script setup>
const isSuccess = ref(false)
const errorMessage = ref('')

const handleSubmit = async () => {
  try {
    await axios.post('/api/users', formData)
    isSuccess.value = true
    errorMessage.value = ''
  } catch (error) {
    isSuccess.value = false
    errorMessage.value = error.response?.data?.message || '提交失败'
  }
}
</script>
<template>
  <div v-if="isSuccess" class="success-message">
    提交成功!
  </div>
  <div v-if="errorMessage" class="error-message">
    {{ errorMessage }}
  </div>
</template>

标签: 表单vue
分享给朋友:

相关文章

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…

vue实现autocomplete

vue实现autocomplete

Vue 实现 Autocomplete 功能 Vue 中实现 Autocomplete(自动完成)功能可以通过自定义组件或使用现有库完成。以下是几种常见方法: 方法一:使用 Vue 原生实现 创建…

vue 实现jqslidedown

vue 实现jqslidedown

在 Vue 中实现类似 jQuery 的 slideDown 效果,可以通过 Vue 的过渡系统或 CSS 动画结合动态样式绑定来实现。以下是几种实现方式: 使用 Vue Transition 组件…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…