Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass Array in $router.push in Vue.js?

I want to pass data through function of VUE to a new page on click of a button. I tried doing this thing using the router method but i was not able to send the array it was just allowing me to send a string with it.

this.$router.push({
    name: "SalesInvoice",
    params: { SalesInvoice: this.stockmaster }
  });

this thing is not working beacuse the stockmaster is an array.

this.$router.push({
    name: "SalesInvoice",
    params: { SalesInvoice: "abc" }
  });

the same thing i did with string and it worked. so is there any way of sending array to next page using vue method.

like image 385
Bhavin Kalal Avatar asked Jan 07 '20 11:01

Bhavin Kalal


People also ask

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.

How do I redirect route Vue?

You can use $router. push({ name: "yourroutename"}) or just router. push("yourroutename") now to redirect.


1 Answers

Just adding my comment as an answer so others can get it easily in the future.

When you are trying to pass an array, object or array of objects to the router, you should first stringify your data with JSON.stringify and the pass it to the router. And then you can access it and parse it back to JSON with JSON.parse. For the particular case above, something like following should be done

this.$router.push({
    name: "SalesInvoice",
    params: { SalesInvoice: JSON.stringify(this.stockmaster) }
  });
like image 103
Muhammad Usman Avatar answered Sep 30 '22 06:09

Muhammad Usman