I have
these simple route/URL when I am in a car details page
http://localhost:8080/car/1
I am using vue2; what is the best way to check if I am on a car
page?
I normally
would have checked for the first segment of the URL, but I wasn't sure if that is the best future-proof approach.
Questions
Should I use JS to detect what page I am ?
Should I use Vue functionality to access the router object?
Why would one decide to pick one over another?
CodePudding user response:
You could provide a name for your route inside the routes
definition like :
{
path: '/car/{id}',
name: 'car',
component: CarView
},
then access it using this.$route.name
or you could parse the this.$route.path
to get the name using String object methods
CodePudding user response:
Perhaps, try using: router.currentRoute.path
, where router is:
import Router from "vue-router";
Vue.use(Router);
const routes = [
{ path: "/", component: Home },
{ path: "/test1", component: Test1 },
{ path: "/test2", component: Test2 }
];
const router = new Router({
routes
});
console.log('Current route: ', router.currentRoute.path);