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/zh-hk/n/370884.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
MIVTX的頭像MIVTX
上一篇 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

發表回復

登錄後才能評論