Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 that data.

I achieved this by simply setting the form action to the new path and manually adding all the URL parameters to the API request.

First I create a simple form with the route /search.

<form action="/search">   <input type="text" name="foobar">   <button type="submit">Submit</button> </form> 

When submitting the form the user leaves the current page and gets redirected to the new page. The URL would now look like this: http://www.example.com/search?foobar=test. Now I fetch the foobar parameter by using this.$route.query.foobar and send it to my API.

However the problem in my approach is when submitting the form the user leaves the current page and a new page load will occur. This is not what we want when building progressive web apps.

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

like image 358
Chris Avatar asked Apr 01 '18 14:04

Chris


People also ask

How do I redirect one page to another in Vue?

EDIT: router.go() changed in VueJS 2.0. You can use $router. push({ name: "yourroutename"}) or just router. push("yourroutename") now to redirect.

How do I redirect a URL in Vue?

Suppose we have a /contact route in our vue app, when a user visits the /contact page we need to redirect them to an external url https://www.google.com/contact/ instead of the same domain redirect. To redirect to an external url in Vue, we can use the window. location. href property.

How do I push my Vue router?

With Vue Router, you can use its router. push() function to programmatically navigate between routes on your site. You can either call push() with a string path, or with an object containing either the path or name of the route.

What is submit stop in VueJS?

Submit Event with Prevent Modifier The other way we can achieve preventing the default form submission behaviour is by attaching a submit event with the prevent modifier to the starting form tag in the template. This will prevent the page from reloading when the submit button is pressed as well.


2 Answers

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

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 <form> 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 <form> behavior. This is similar to using event.stopPropagation(); and event.preventDefault(); in jQuery

<form>   <input type="text" name="foobar" v-model="foobar">   <button type="submit" @click.stop.prevent="submit()">Submit</button> </form> 

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);       }     }   } 
like image 198
divine Avatar answered Sep 26 '22 09:09

divine


 submitClick(){         this.$router.push({path: '/search', query:{key: value}})     }
<form @submit.stop.prevent="submitClick">        <input v-model="keyword">        <button type="submit">Search</button> </form>
This is the right way to achieve a SPA with form submit. it supports enter key and in submitClick, there can be any logic before submitting
like image 37
Divyanshu Pandey Avatar answered Sep 23 '22 09:09

Divyanshu Pandey