VueJs get url query

Vuejs2Vue Componentvue.jsVue Router

Vuejs2 Problem Overview


I'm developing a website with vuejs and at this moment I'm with a problem, I need to get the URL query (page) from a URL like this websitename.com/user/?page=1 but the this.$router.query.page gives me an error (Uncaught TypeError: Cannot read property 'page' of undefined)

Does someone know something about this problem and can help me?

PS: I can set the query page using

this.$router.push({name: 'userIndex', query: { page: '123' } });

and I can get the URL normal params like the

userID -> (websitename.com/user/:userId | websitename.com/user/1)

but I can't get any query parameter.

Vuejs2 Solutions


Solution 1 - Vuejs2

I think you can simple call like this, this will give you result value.

this.$route.query.page

Look image $route is object in Vue Instance and you can access with this keyword and next you can select object properties like above one :

enter image description here

Have a look Vue-router document for selecting queries value :

Vue Router Object

Solution 2 - Vuejs2

You can also get them with pure javascript.

For example:

new URL(location.href).searchParams.get('page')

For this url: websitename.com/user/?page=1, it would return a value of 1

Solution 3 - Vuejs2

Current route properties are present in this.$route, this.$router is the instance of router object which gives the configuration of the router. You can get the current route query using this.$route.query

Solution 4 - Vuejs2

In my case I console.log(this.$route) and returned the fullPath:

console.js:
fullPath: "/solicitud/MX/666",
params: {market: "MX", id: "666"},
path: "/solicitud/MX/666"

console.js: /solicitud/MX/666

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSimão LemosView Question on Stackoverflow
Solution 1 - Vuejs2Mandeep GillView Answer on Stackoverflow
Solution 2 - Vuejs2Brad AhrensView Answer on Stackoverflow
Solution 3 - Vuejs2Srinivas DamamView Answer on Stackoverflow
Solution 4 - Vuejs2Jorge EpuñanView Answer on Stackoverflow