How to submit a form in Vue, redirect to a new route and pass the parameters?

前端 未结 3 1154
终归单人心
终归单人心 2021-02-05 04:52

I am using Nuxt and Vue and I am trying to submit a form, redirect the user to a new route including the submitted params, send an API request to get some data and then render t

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 05:45

    The default behavior of

    is to reload the page onsubmit. When implementing SPA's it would be better to avoid invoking default behavior of .

    Making use of router module which is available out-of-box in nuxtjs will enable all the redirection controls to flow within the application. if we try to trigger events available via then browser reload will occur. This has to be avoided.

    So my question is how can I submit a form in Nuxt/Vue and redirect to a new route including the submitted parameters?

    You can try below approach

    First

    Use .stop.prevent modifiers to prevent the submit button from invoking default behavior. This is similar to using event.stopPropagation(); and event.preventDefault(); in jQuery

    
      
      
    
    

    Then

    Create

    1. vue model object foobar

    2. vue method submit

    Use this.$router.push to redirect to next page. This will enable the control flow to stay inside the SPA. if you want to send any data into server then you can do it before invoking this.$router.push else you can redirect and continue your logic.

    export default {
        data(){
          return {
            foobar : null
          }
        },
        methods: {
          submit(){
             //if you want to send any data into server before redirection then you can do it here
            this.$router.push("/search?"+this.foobar);
          }
        }
      }
    

提交回复
热议问题