How can I get query parameters from a URL in Vue.js?

How can I fetch query parameters in Vue.js?


Can’t find a way to fetch or do I need to use pure JS or some library for this?

According to the docs of route object, you have access to a $route object from your components, that expose what you need. In this case

//from your component
console.log(this.$route.query.test) // outputs 'yay'

Without vue-route, split the URL

var vm = new Vue({
    let uri = window.location.href.split('?');
    if (uri.length == 2)
      let vars = uri[1].split('&');
      let getVars = {};
      let tmp = '';
        tmp = v.split('=');
        if(tmp.length == 2)
        getVars[tmp[0]] = tmp[1];
      // do 

Another solution

var vm = new Vue({
    let uri =; 
    let params = new URLSearchParams(uri);

More detailed answer to help the newbies of VueJS:

  • First define your router object, select the mode you seem fit. You can declare your routes inside the routes list.
  • Next you would want your main app to know router exists, so declare it inside the main app declaration .
  • Lastly they $route instance holds all the information about the current route. The code will console log just the parameter passed in the url. (*Mounted is similar to document.ready , .ie its called as soon as the app is ready)

And the code itself:

<script src=""></script>
var router = new VueRouter({
    mode: 'history',
    routes: []
var vm =  new Vue({
    el: '#app',
    mounted: function() {
        q = this.$route.query.q

Another way (assuming you are using vue-router), is to map the query param to a prop in your router. Then you can treat it like any other prop in your component code. For example, add this route;

    path: '/mypage', 
    name: 'mypage', 
    component: MyPage, 
    props: (route) => ({ foo: })  

Then in your component you can add the prop as normal;

props: {
    foo: {
        type: String,
        default: null

Then it will be available as and you can do anything you want with it (like set a watcher, etc.)

As of this date, the correct way according to the dynamic routing docs is:


instead of


You can use vue-router.I have an example below:


new Vue({
  el: "#app",
  data: {
    name: '',
    lastName: ''
  beforeRouteEnter(to, from, next) {
      if(Object.keys(to.query).length !== 0) { //if the url has query (?query)
        next(vm => { =
         vm.lastName = to.query.lastName

Note: In beforeRouteEnter function we cannot access the component's properties like: this.propertyName.That's why i have pass the vm to next function.It is the recommented way to access the vue instance.Actually the vm it stands for vue instance

If your server uses php, you can do this:

const from = '<?php echo $_GET["from"];?>';

Just works.

You can get By Using this function.


If your url looks something like this:

Where '123' is a parameter named 'id' (url like /something/:id), try with: