Table of Contents
What are query parameters
Consider the following URL:
1http://localhost:5173/?user=alice&age=30
In the above URL, user=alice
and age=30
are called query parameters.
There are couple of ways in which we can access query parameters in Vue. js. We will see both of them.
Using vue router
If you do not have Vue router installed in your application, then do so by running the following command:
1npm install vue-router@4
Now configure the routes in main.js
file:
1import { createApp } from 'vue';2import App from './App.vue';3import "./style.css";4import { createRouter, createWebHistory } from 'vue-router';56const routes = [7 { path: '/', component: App },8];910const router = createRouter({11 history: createWebHistory(),12 routes,13});1415const app = createApp(App);16app.use(router);17app.mount('#app');
Now you can access the query parameters using the following code in your Vue component:
1<template>2 <div>3 <h1>User Details</h1>4 <p>User: {{ route.query.user }}</p>5 <p>Age: {{ route.query.age }}</p>6 </div>7</template>89<script>10import { useRoute } from "vue-router";1112export default {13 setup() {14 const route = useRoute(); // Access the current route and query params1516 return {17 route,18 };19 },20};21</script>
If you want to update the query parameters manually, you can do so as shown below:
1<template>2 <div>3 <h1>Update Query Parameters</h1>4 <!-- Button that triggers the updateQueryParams method -->5 <button @click="updateQueryParams">Update Query Params</button>6 </div>7</template>89<script>10import { useRouter } from 'vue-router';1112export default {13 setup() {14 const router = useRouter();1516 const updateQueryParams = () => {17 router.push({ query: { user: 'alice', age: 30 } });18 };1920 return {21 updateQueryParams,22 };23 },24};25</script>
Using the Web API: URLSearchParams
If you don't have Vue router installed and do not want to use Vue router, then you can leverage the URLSearchParams Web API as shown below:
1<template>2 <div>3 <h1>User Details</h1>4 <p>User: {{ user }}</p>5 <p>Age: {{ age }}</p>6 </div>7</template>89<script>10export default {11 data() {12 return {13 user: "",14 age: "",15 };16 },17 mounted() {18 const queryParams = new URLSearchParams(window.location.search);19 this.user = queryParams.get("user") || "Guest";20 this.age = queryParams.get("age") || "Unknown";21 },22};23</script>
You can use the the window.history
to update the URL parameters, without needing to use Vue router:
1<template>2 <div>3 <h1>Update Query Parameters</h1>4 <button @click="updateQueryParams">Update Query Params</button>5 </div>6</template>78<script>9export default {10 methods: {11 updateQueryParams() {12 // Get the current query parameters from the URL13 const params = new URLSearchParams(window.location.search);1415 // Set new query parameters or update existing ones16 params.set("user", "alice");17 params.set("age", "30");1819 // Construct the new URL20 const newUrl = `${window.location.pathname}?${params.toString()}`;2122 window.history.pushState(null, "", newUrl);2324 },25 },26};27</script>
Do follow me on twitter where I post developer insights more often!
Leave a Comment