『解疑』vue路由hash模式在qq网页分享中截断#号后的hash值问题解决

问题

场景:
在使用qq网页分享中,分享的网页采用的是vue的单页面应用,路由采用的是哈希模式,由于浏览器会自动截断#号后的hash值,导致分享后的链接不全

  • 分享的链接:
    http://127.0.0.1:8080/#/reportEdit/test01/test003?spName=test01&dpName=test003
  • 拿到的链接:
    http://127.0.0.1:8080

解决

解决方案有两个:

  1. 直接使用encodeURIComponent,对分享的链接进行编码。
  2. 借用一个html进行路由重定向。
方案一:
  • 直接使用encodeURIComponent,对分享的链接进行编码。

encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。 —w3school

// qq分享方法
    shareQQFun() {
      let shareUrl = location.href.split('#')[0] + '#/reportView' + '?spName=' + getQueryString("spName") + '&dpName=' + getQueryString("dpName")
      let currentUrl = encodeURIComponent(shareUrl)
      console.log('[qq分享内容]',shareUrl)

      let common = {
        url: currentUrl,
        showcount: '会话',
        title: '标题',
        summary: '',
        desc: '描述',
        pics: ''
      }
      //地址拼接参数
      var urlData = `https://connect.qq.com/widget/shareqq/index.html?url=${common.url}&showcount=${common.showcount}&desc=${common.showcount}&summary=${common.summary}&title=${common.title}&pics=${common.pics}&style=203&width=19&height=22`
      window.open(urlData, '_blank')
    }
方案二:
  • 借用一个html进行路由重定向。

步骤:

  1. 在static内新建一个html,命名为redirect.html
  2. 在redirect.html内编写重定向业务逻辑(以下提供最简易重定向代码)
  3. 重写分享链接

redirect.html代码:

<!-- 用于重定向 -->
<script>
    let url = location.href.split('redirectUrl=')
    if(url[1]){
        window.location = url[1]
    }
</script>

分享方法代码:

// qq分享方法
    shareQQFun() {
      let shareUrl = location.href.split('#')[0] + '#/reportView' + '?spName=' + getQueryString("spName") + '&dpName=' + getQueryString("dpName")
     let currentUrl = location.href.split('#')[0] + 'static/redirect.html?redirectUrl=' + shareUrl
      console.log('[qq分享内容]',shareUrl)
      
      let common = {
        url: currentUrl,
        showcount: '会话',
        title: '标题',
        summary: '',
        desc: '描述',
        pics: ''
      }
      //地址拼接参数
      var urlData = `https://connect.qq.com/widget/shareqq/index.html?url=${common.url}&showcount=${common.showcount}&desc=${common.showcount}&summary=${common.summary}&title=${common.title}&pics=${common.pics}&style=203&width=19&height=22`
      window.open(urlData, '_blank')
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值