[Vuejs]-Get parameter from URL in Vue JS

0👍

let id = window.location.href.split('/').slice(-2)[0]

0👍

Since you’re already rendering the page in a Blade file, you should already have access to the user ID server side.

In that case, simply add the variable server side. For example:

<a :href="'https://domain.test/admin/users/dashboard/user/' + {{ 
$user->id }} + '/show'">Go to Dashboard</a>

0👍

To get parmeters from url you can use vue router for that like this:

const routes = [
    // dynamic segments start with a colon
    { path: '/users/:id', component: User },
  ]

const User = {
  template: '<div>User {{ $route.params.id }}</div>',
}

Here is the link for more examples and vue router documentation:

https://router.vuejs.org/guide/essentials/dynamic-matching.html

You can also get parameters with only vanilla JavaScripe like follow:

const queryString = window.location.search;
console.log(queryString);
// ?product=shirt&color=blue&newuser&size=m

//then you have to parse the url
const urlParams = new URLSearchParams(queryString);

const color = urlParams.get('color')
console.log(color);
// blue

Leave a comment