Vue.js 获取页面路由地址方法详解
在Vue.js中,可以通过$router对象获取页面的路由地址。
-
在Vue组件中使用
this.$route.path来获取当前路由地址。 -
在Vue组件中使用
this.$router.currentRoute.path来获取当前路由地址。 -
在Vue实例中使用
this.$router.currentRoute.path来获取当前路由地址。 -
在Vue路由钩子函数中,可以通过
to参数获取将要跳转的路由地址。
例如,在Vue组件中获取当前路由地址的代码如下:
export default {
mounted() {
console.log(this.$route.path); // 获取当前路由地址
}
}
在Vue路由钩子函数中获取将要跳转的路由地址的代码如下:
const router = new VueRouter({
routes: [
{
path: '/home',
component: Home
},
{
path: '/about',
component: About,
beforeEnter(to, from, next) {
console.log(to.path); // 获取将要跳转的路由地址
next();
}
}
]
})
原文地址: https://www.cveoy.top/t/topic/nXFZ 著作权归作者所有。请勿转载和采集!