VueJS 路由器中的 `path` 和 `fullPath` 有什么区别?
What is the difference between `path` and `fullPath` in VueJS router?
在我的 router.js
文件中,当我使用 beforeEach
方法时,我在 to
的属性中得到 path
和 fullPath
并且from
参数。我想知道我应该使用哪一个来进行重定向。我见过两者都被使用过,但我不知道什么时候使用哪个,两者之间有什么区别。
一个例子:
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()
}
},
}]
})
- $route.fullPath
- type:
string
The full resolved URL including query and hash.
- $route.path
- type:
string
A string that equals the path of the current route, always resolved as an absolute path. e.g. "/foo/bar".
path: 字符串,等于当前路由的路径,
始终解析为绝对路径。
示例:/user/11/posts、/user/37/posts
fullPath:完整的 URL,包括查询和哈希。
其他...
params:包含键/值对的对象
段。
query:一个包含键/值对的对象
url 值字符串。例如,对于
/ 富?用户 = 1,我们有 $ route.query.user == 1。
hash:当前路径的哈希值(没有#),如果存在的话。如果
没有散列,值将是一个字符串
空的。
matched: 包含所有路由记录的数组
当前路线的嵌套路径段。这
路由记录是对象的副本
路由配置。
name: 当前路由的名称,如果存在的话。
在我的 router.js
文件中,当我使用 beforeEach
方法时,我在 to
的属性中得到 path
和 fullPath
并且from
参数。我想知道我应该使用哪一个来进行重定向。我见过两者都被使用过,但我不知道什么时候使用哪个,两者之间有什么区别。
一个例子:
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()
}
},
}]
})
- $route.fullPath
- type:
string
The full resolved URL including query and hash.- $route.path
- type:
string
A string that equals the path of the current route, always resolved as an absolute path. e.g. "/foo/bar".
path: 字符串,等于当前路由的路径, 始终解析为绝对路径。 示例:/user/11/posts、/user/37/posts
fullPath:完整的 URL,包括查询和哈希。
其他...
params:包含键/值对的对象 段。 query:一个包含键/值对的对象 url 值字符串。例如,对于 / 富?用户 = 1,我们有 $ route.query.user == 1。 hash:当前路径的哈希值(没有#),如果存在的话。如果 没有散列,值将是一个字符串 空的。 matched: 包含所有路由记录的数组 当前路线的嵌套路径段。这 路由记录是对象的副本 路由配置。 name: 当前路由的名称,如果存在的话。