vue - url路径
js
//https://home.istr.cn:8443/tiku/login?a=1#/home
window.location
{
"ancestorOrigins": {},
"href": "https://home.istr.cn:8443/tiku/login#/home",
"origin": "https://home.istr.cn:8443",
"protocol": "https:",
"host": "home.istr.cn:8443",
"hostname": "home.istr.cn",
"port": "8443",
"pathname": "/tiku/login",
"search": "?a=1",
"hash": "#/home"
}// 1. 当前页面完整路径(登录成功后要跳回来的地址)
const targetRoute = router.currentRoute.value.fullPath
// 2. 用 VueRouter 生成 redirectUrl(自动处理 base/subpath)
const redirectPath = router.resolve({
path: '/third-login',
query: {
target: targetRoute,
},
})
console.log('生成的重定向地址:', JSON.stringify(redirectPath))router.resolve():VueRouter 提供的路径解析工具,作用是:
- 帮你拼接路由
- 自动处理项目的
base基础路径(比如项目部署在子目录) - 自动编码特殊字符(防止 URL 出错)
json
{
"fullPath": "/third-login?target=/login",
"hash": "",
"query": {
"target": "/login"
},
"name": "thirdLogin",
"path": "/third-login",
"params": {},
"matched": [
{
"path": "/third-login",
"name": "thirdLogin",
"meta": {"title": "第三方登录", "isHide": true},
"props": {"default": false},
"children": [],
"instances": {},
"leaveGuards": {},
"updateGuards": {},
"enterCallbacks": {},
"components": {}
}
],
"meta": {"title": "第三方登录", "isHide": true},
"href": "/tiku/third-login?target=/login"
}