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?
would have checked for the first segment of the URL, but I wasn't sure if that is the best future-proof approach.
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?
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
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.