this.$router.resolve() 方法 (非 hash 模式 window.open() 打开页面)

【问题描述】

  • 路由模式从 hash 模式换为 history 模式 window.open() 不能跳转了

【解决】

  • 借助 this.$router.resolve() 方法 给路径拼接上 #/

  • const { href } = this.$router.resolve({path:`/home/?aaa=${ev.aaa}&bbb=${ev.bbb}`})
    window.open(href, '_blank') // _blank 打开新的页面
    

【补充】this.$router.resolve({ path: '123' }) 返回值:

  • {
        "location": {
            "_normalized": true,
            "path": "/123",
            "query": {},
            "hash": "",
            "params": {}
        },
        "route": {
            "name": null,
            "meta": {},
            "path": "/123",
            "hash": "",
            "query": {},
            "params": {},
            "fullPath": "/123",
            "matched": []
        },
        "href": "#/123",  // 【要用】
        "normalizedTo": {
            "_normalized": true,
            "path": "/123",
            "query": {},
            "hash": "",
            "params": {}
        },
        "resolved": {
            "name": null,
            "meta": {},
            "path": "/123",
            "hash": "",
            "query": {},
            "params": {},
            "fullPath": "/123",
            "matched": []
        }
    }
    

【设想】

手动在 window.open() 中的路径前加上 #/ 应该也可以(但是测试环境不知道返回的 href 是什么,可能不一样)

【补充】路由不能跳转可能还有一个原因

window.open() 跳转的连接需要加上 http://

window.open('www.baidu.com') // 跳转不过去,跳转的地址是 http://localhost:8080/guangxi-corps/www.baidu.com 为 404
window.open('http://www.baidu.com') // 可以跳转
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值