在Vue.js中,可以通过$router对象获取页面的路由地址。

  1. 在Vue组件中使用this.$route.path来获取当前路由地址。

  2. 在Vue组件中使用this.$router.currentRoute.path来获取当前路由地址。

  3. 在Vue实例中使用this.$router.currentRoute.path来获取当前路由地址。

  4. 在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();
      }
    }
  ]
})
Vue.js 获取页面路由地址方法详解

原文地址: https://www.cveoy.top/t/topic/nXFZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录