How do I pass Array in $router.push in Vue.js?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How Do I Pass an Array in $router.push in Vue.js? A Developer's Guide
Passing complex data structures, like arrays, through Vue Router parameters (params) can often lead to confusion. As you experienced, the router’s parameter system is fundamentally designed for simple string identifiers, making it unsuitable for direct storage of complex objects or arrays.
This guide will walk you through the correct, robust methods for transferring array data to a new route in your Vue application, moving beyond simple string parameters to implement scalable data handling.
Why Direct Array Passing Fails in Route Params
The core issue lies in how Vue Router handles navigation via params. The structure { name: 'RouteName', params: { key: value } } is designed for defining dynamic segments of a URL (e.g., /users/:id). When you try to assign an array directly, the router attempts to serialize it into a single string within that parameter, which often results in data loss or improper parsing when retrieving it on the destination page.
As demonstrated in your attempt:
// This fails because 'this.stockmaster' is an Array
this.$router.push({
name: "SalesInvoice",
params: { SalesInvoice: this.stockmaster } // Problematic
});
The router doesn't know how to natively handle the array structure within the URL parameters cleanly. To solve this, we need to use methods that are explicitly designed for passing complex data in URLs: Query Parameters.
Method 1: Passing Arrays Using Query Parameters (The Recommended Approach)
For passing lists or arrays of items—which is common when navigating based on filtered results or a list selection—the most idiomatic approach in web development is using URL query strings (?key=value&key2=value2).
This method keeps the data visible in the URL, making it bookmarkable and shareable, while correctly separating the data from the route structure.
Implementation Steps
- Serialize the Array: Convert your array into a format that can be safely encoded for a URL. JSON stringification is the most reliable way to handle complex objects or arrays.
- Append to Router: Use the
queryproperty of$router.push()instead ofparams.
Here is how you implement this in your Vue component:
export default {
data() {
return {
stockmaster: [
{ id: 1, name: 'Item A' },
{ id: 2, name: 'Item B' },
{ id: 3, name: 'Item C' }
]
};
},
methods: {
navigateToInvoice() {
// 1. Serialize the array into a JSON string
const stockmasterJson = JSON.stringify(this.stockmaster);
// 2. Use query parameters to pass the data
this.$router.push({
name: "SalesInvoice",
query: {
invoiceItems: stockmasterJson // Passing the array as a stringified JSON
}
});
}
}
}
Destination Page Retrieval:
On the target page (SalesInvoice), you retrieve the data from $route.query:
// On the SalesInvoice component
mounted() {
this.loadData();
}
methods: {
loadData() {
const query = this.$route.query;
if (query && query.invoiceItems) {
try {
// 1. Parse the JSON string back into a JavaScript array
const items = JSON.parse(query.invoiceItems);
console.log("Received items:", items);
// Now you can use 'items' to populate your view!
} catch (error) {
console.error("Error parsing item data:", error);
}
}
}
}
Method 2: Using State Management for Large Datasets
While query parameters are excellent for moderate amounts of data or simple lists, if you are dealing with very large arrays (hundreds or thousands of records) or complex state that needs to persist across many routes, passing it via the URL is inefficient and pollutes the history.
In these scenarios, a better architectural approach is to utilize Vuex or Pinia for global state management. When a user clicks a button, you update the central store with the necessary data, and then navigate to the new route. This keeps your URL clean and delegates complex data handling to dedicated state tools, which aligns well with robust backend principles often seen in systems like those built with Laravel where data integrity is paramount.
Conclusion
To successfully pass arrays in Vue Router navigation, avoid using params for complex objects. Instead, leverage the query object within $router.push(), ensuring you serialize your array into a JSON string before passing it. For truly large or highly dynamic datasets, always prioritize centralized state management tools like Pinia to maintain a clean separation between routing concerns and application data.