Vue表单提交实践与总结

一、vue提交表单没反应

1、检查表单是否绑定到正确的数据

  <form v-on:submit.prevent="submitForm">
      <!-- ... -->
  </form>
   
  Vue.component({
      data: function() {
          return { form: {} };
      },
      methods: {
          submitForm: function() {
              console.log(this.form);
          }
      }
  });

2、确保提交表单的方法已正确绑定

  <form v-on:submit.prevent="submitForm">
      <!-- ... -->
  </form>
   
  Vue.component({
      methods: {
          submitForm: function() {
              console.log('form submitted');
          }
      }
  });
 

3、查看前端和后端的请求和响应情况,看是否存在问题

二、vue提交表单后如何返回到上一页

1、在返回按钮中使用history.go(-1)方法回到上一页

  <button v-on:click="goBack"> 返回 </button>
   
  Vue.component({
      methods: {
          goBack: function() {
              history.go(-1);
          }
      }
  });

2、在页面中使用vue-router进行跳转实现页面返回

  <router-link to="/"> 返回 </router-link>
 

三、vue提交表单数据

1、使用v-model绑定数据

    <input v-model="form.userName">
    <input v-model="form.email">
    <button v-on:click="submitForm">提交</button>

    Vue.component({
        data: function () {
            return {
                form: {
                    userName: '',
                    email: ''
                }
            }
        },
        methods: {
            submitForm: function () {
                console.log(form);
            }
        }
    });
 

2、使用ref获取表单元素的值

    <input ref="userName">
    <input ref="email">
    <button v-on:click="submitForm">提交</button>
   
    Vue.component({
        methods: {
            submitForm: function () {
                console.log(this.$refs.userName.value);
                console.log(this.$refs.email.value);
            }
        }
    });
 

四、vue提交表单时怎么校验信息

1、使用vuelidate进行验证

    <input v-model="form.userName">
    <input v-model="form.email">
    <button v-on:click="submitForm">提交</button>

    Vue.component({
        data: function () {
            return {
                form: {
                    userName: '',
                    email: ''
                }
            }
        },
        validations: {
            form: {
                email: {
                    required,
                    email
                },
                userName: {
                    required,
                    minLength: minLength(3)
                }
            }
        },
        methods: {
            submitForm: function () {
                this.$v.form.$touch();
                if (!this.$v.form.$error) {
                    console.log('submit success');
                }
            }
        }
    });
 

2、手动验证信息

    <input v-model="form.userName">
    <input v-model="form.email">
    <button v-on:click="submitForm">提交</button>

    Vue.component({
        data: function () {
            return {
                form: {
                    userName: '',
                    email: ''
                },
                errors: {}
            }
        },
        methods: {
            validate: function () {
                this.errors = {};
                if (!this.form.userName) {
                    this.errors.userName = '用户名不能为空';
                }
                if (!this.form.email) {
                    this.errors.email = 'Email不能为空';
                }
                return Object.keys(this.errors).length === 0;
            },
            submitForm: function () {
                if (this.validate()) {
                    console.log('submit success');
                }
            }
        }
    });
 

五、vue提交表单数据到后端

1、使用axios发送表单数据

    <form v-on:submit.prevent="submitForm">
        <!-- ... -->
    </form>

    Vue.component({
        methods: {
            submitForm: function () {
                axios.post('url', this.form).then(function (response) {
                    console.log(response);
                }).catch(function (error) {
                    console.log(error);
                });
            }
        }
    });
 

2、使用fetch发送表单数据

    <form v-on:submit.prevent="submitForm">
        <!-- ... -->
    </form>

    Vue.component({
        methods: {
            submitForm: function () {
                var data = this.form;
                var formData = new FormData();
                for (var key in data) {
                    formData.append(key, data[key]);
                }
                fetch('url', {
                    method: 'POST',
                    body: formData
                }).then(function (response) {
                    console.log(response);
                }).catch(function (error) {
                    console.log(error);
                });
            }
        }
    });
 

六、vue提交表单、后端map接收

1、使用json的方式发送表单数据,并在后端进行json解析

    <form v-on:submit.prevent="submitForm">
        <!-- ... -->
    </form>

    Vue.component({
        methods: {
            submitForm: function () {
                axios.post('url', JSON.stringify(this.form)).then(function (response) {
                    console.log(response);
                }).catch(function (error) {
                    console.log(error);
                });
            }
        }
    });
 

2、使用springmvc将表单数据绑定到map中

     @RequestMapping("/saveUserInfo")
     @ResponseBody
     public String saveUserInfo(@RequestBody Map userInfo) {
         System.out.println(userInfo.get("userName"));
         System.out.println(userInfo.get("email"));
         return "success";
     }
 

七、vue提交表单数据到邮箱

1、使用后端发送邮件的工具类发送邮件

     public void sendMail(String subject, String content, String to) {
         JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
         mailSender.setUsername("用户名");
         mailSender.setPassword("密码");
         mailSender.setHost("smtp.qq.com");
         mailSender.setPort(587);

         Properties properties = new Properties( );
         properties.put("mail.smtp.auth", "true");
         properties.put("mail.smtp.starttls.enable", "true");
         properties.put("mail.smtp.timeout", 10000);
         mailSender.setJavaMailProperties(properties);

         MimeMessage message = mailSender.createMimeMessage( );
         MimeMessageHelper helper = new MimeMessageHelper(message);
         helper.setSubject(subject);
         helper.setFrom("发件人");
         helper.setText(content, true);
         helper.setTo(to);

         mailSender.send(message);
     }
 

2、 使用第三方的邮件发送服务发送邮件

     axios.get('http://api.sendcloud.net/apiv2/mail/send', {
         params: {
             apiUser: apiUser,
             apiKey: apiKey,
             to: to,
             from: from,
             fromName: senderName,
             subject: subject,
             html: content
         }
     }).then(function(response) {
         console.log(response);
     }).catch(function(error) {
         console.log(error);
     });
 

八、vue form表单提交

    <template>
        <form v-on:submit.prevent="submitForm">
            <label> Name: <input v-model="form.name"> </label>
            <label> Email: <input v-model="form.email"> </label>
            <button type="submit"> Submit </button>
        </form>
    </template>

    Vue.component({
        data: function () {
            return {
                form: {
                    name: '',
                    email: ''
                }
            }
        },
        methods: {
            submitForm: function () {
                console.log('form submitted');
            }
        }
    });
 

九、vue表单

    <template>
        <form>
            <div class="form-group">
                <label for="name"> Name: </label>
                <input type="text" class="form-control" id="name" v-model="form.name">
            </div>
            <div class="form-group">
                <label for="email"> Email: </label>
                <input type="email" class="form-control" id="email" v-model="form.email">
            </div>
            <button type="submit" class="btn btn-primary" v-on:click="submitForm"> Submit </button>
        </form>
    </template>

    Vue.component({
        data: function () {
            return {
                form: {
                    name: '',
                    email: ''
                }
            }
        },
        methods: {
            submitForm: function () {
                console.log('form submitted');
            }
        }
    });
 

十、vue获取表单数据

1、使用v-model绑定数据并在submitForm方法中获取

    <div id="app">
        <input v-model="name">
        <input v-model="email">
        <button v-on:click="submitForm">提交</button>
    </div>

    new Vue({
        el: '#app',
        data: {
            name: '',
            email: ''
        },
        methods: {
            submitForm: function () {
                console.log(this.name);
                console.log(this.email);
            }
        }
    });
 

2、使用ref获取表单元素的值

    <div id="app">
        <input ref="name">
        <input ref="email">
        <button v-on:click="submitForm">提交</button>
    </div>

    new Vue({
        el: '#app',
        methods: {
            submitForm: function () {
                console.log(this.$refs.name.value);
                console.log(this.$refs.email.value);
            }
        }
    });
 

以上是关于vue表单提交的实践与总结,稍微有些繁琐,但希望大家在实际项目中能够根据需求有针对性的使用各种提交方式。

原创文章,作者:MIVTX,如若转载,请注明出处:https://www.506064.com/n/370884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
MIVTXMIVTX
上一篇 2025-04-23 00:48
下一篇 2025-04-23 00:48

相关推荐

  • 使用Vue实现前端AES加密并输出为十六进制的方法

    在前端开发中,数据传输的安全性问题十分重要,其中一种保护数据安全的方式是加密。本文将会介绍如何使用Vue框架实现前端AES加密并将加密结果输出为十六进制。 一、AES加密介绍 AE…

    编程 2025-04-29
  • Vue TS工程结构用法介绍

    在本篇文章中,我们将从多个方面对Vue TS工程结构进行详细的阐述,涵盖文件结构、路由配置、组件间通讯、状态管理等内容,并给出对应的代码示例。 一、文件结构 一个好的文件结构可以极…

    编程 2025-04-29
  • Java表单提交方式

    Java表单提交有两种方式,分别是get和post。下面我们将从以下几个方面详细阐述这两种方式。 一、get方式 1、什么是get方式 在get方式下,表单的数据会以查询字符串的形…

    编程 2025-04-27
  • Vue3的vue-resource使用教程

    本文将从以下几个方面详细阐述Vue3如何使用vue-resource。 一、安装Vue3和vue-resource 在使用vue-resource前,我们需要先安装Vue3和vue…

    编程 2025-04-27
  • Vue模拟按键按下

    本文将从以下几个方面对Vue模拟按键按下进行详细阐述: 一、Vue 模拟按键按下的场景 在前端开发中,我们常常需要模拟按键按下的场景,比如在表单中填写内容后,按下“回车键”提交表单…

    编程 2025-04-27
  • ThinkPHP6 + Vue.js: 不使用Fetch的数据请求方法

    本文将介绍如何在ThinkPHP6和Vue.js中进行数据请求,同时避免使用Fetch函数。 一、AJAX:XMLHttpRequest的基础使用 在进行数据请求时,最基础的方式就…

    编程 2025-04-27
  • 开发前端程序,Vue是否足够?

    Vue是一个轻量级,高效,渐进式的JavaScript框架,用于构建Web界面。开发人员可以使用Vue轻松完成前端编程,开发响应式应用程序。然而,当涉及到需要更大的生态系统,或利用…

    编程 2025-04-27
  • 如何在Vue中点击清除SetInterval

    在Vue中点击清除SetInterval是常见的需求之一。本文将介绍如何在Vue中进行这个操作。 一、使用setInterval和clearInterval 在Vue中,使用set…

    编程 2025-04-27
  • VueClearable:实现易于清除的Vue输入框

    一、VueClearable基本介绍 VueClearable是一个基于Vue.js开发的易于清除的输入框组件,可以在输入框中添加“清除”按钮,使得用户可以一键清空已输入内容,提升…

    编程 2025-04-25
  • Vue 往数组添加字母key

    本文将详细阐述如何在 Vue 中往数组中添加字母 key,并从多个方面探讨实现方法。 一、Vue 中添加字母 key 的实现方法 在 Vue 中,添加 key 可以使用 v-bin…

    编程 2025-04-25

发表回复

登录后才能评论