Using event modifier .prevent in Vue to submit form without redirection

后端 未结 3 1529
日久生厌
日久生厌 2021-01-05 01:33

I\'m using Vue in my application and I would like to know how to submit a form but avoid redirection. According to the official Vue doc it can be achieved in the following w

3条回答
  •  一个人的身影
    2021-01-05 01:57

    onSubmit should be a function within your methods property on the vue instance. I.e

    methods: {
      onSubmit () {
        // DO Something
      }
    }
    

    Then as the docs say, using

    is correct. I use this in my app.

    See the example below. The message will change once the button is clicked without a page refresh.

    var vm = new Vue({
      el: '#app',
      data () {
        return {
          test: 'BEFORE SUBMIT'
        }
      },
      methods: {
        onSubmit () {
          this.test = 'AFTER SUBMIT'
        }
      }
    });
    
    
    {{test}}

提交回复
热议问题