首页 > 解决方案 > VueJS路由器中的`path`和`fullPath`有什么区别?

问题描述

在我的router.js文件中,当我使用该beforeEach方法时,我会在和参数的属性中获取path和。我想知道我应该使用哪一个来进行重定向。我已经看到两者都被使用了,但我不知道何时使用哪个,两者之间有什么区别。fullPathtofrom

一个例子:

export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [{
        path: 'login'
        beforeEnter: (to, from, next) => {
            console.log(to, from) // the routes
            if (User.loggedIn()) {
                next(from.fullPath) // or maybe `from.path`
            } else {
                next()
            }
        },
    }]
})

标签: javascriptvuejs2vue-router

解决方案


来自Vue API 参考

  • $route.fullPath
    • type:string
      完整解析的 URL,包括查询和哈希。
  • $route.path
    • type:string
      一个字符串,等于当前路由的路径,总是解析为绝对路径。例如“/foo/bar”。

推荐阅读