vue单页面在微信下只能分享落地页的解决方案

作者:ci0n 时间:2024-05-09 10:52:19 

实际上关键词叫 微信pushState只能分享落地页 更贴切一点

应用场景:

  1. vue + vue-router

  2. vue-router使用hash模式(history模式没试过)

  3. 不使用微信的js-sdk(因为我这个项目是可配置域名的商城,比较特殊,不能使用微信sdk)

这个方案并不是最优秀的,会对性能造成一定的影响

HTML5 history.pushState

vue-router的内部是通过 history.pushState 和 history.replaceState 实现的。但是iOS设备的微信浏览器不会去检测它们的变化。但是我们可以通过更新 location.href 让微信浏览器识别到当前的url。


// vue-router/src/util/push-state.js

export function pushState (url?: string, replace?: boolean) {
saveScrollPosition()
// try...catch the pushState call to get around Safari
// DOM Exception 18 where it limits to 100 pushState calls
const history = window.history
try {
 if (replace) {
  history.replaceState({ key: _key }, '', url)
 } else {
  _key = genKey()
  history.pushState({ key: _key }, '', url)
 }
} catch (e) {
 window.location[replace ? 'replace' : 'assign'](url)
}
}

export function replaceState (url?: string) {
pushState(url, true)
}

解决方法

window.location.href = window.location.href ,这段代码可以让微信记录当前的url,且不会刷新页面。可以在app.vue中 watch $route 在每次页面更新的时候执行一次。


// app.vue

watch: {
$route: {
 immediate: true,
 deep: true,
 handler(to) {
  // 微信浏览器判断
  const WECHAT_BROWSER = navigator.userAgent.toLowerCase().includes('micromessenger')

// 解决iOS微信浏览器分享地址只能是落地页的问题,这个操作是不会刷新页面的,query参数改变也会执行
  if (WECHAT_BROWSER) {
   // eslint-disable-next-line
   window.location.href = window.location.href
  }
 }
},

使用了上述方法可以解决这个问题,但是这会引出一个很奇葩的问题,在真机上进入 http://192.168.1.5:8080 和 http://192.168.1.5:8080/#/ 这两个页面,其中有一个链接的bug依然存在。原因具体不清楚,经过测试可以在入口文件(main.js)中在页面还没有展示内容前刷新一次页面,即可解决这个问题。


// main.js

// 微信浏览器判断
const WECHAT_BROWSER = navigator.userAgent.toLowerCase().includes('micromessenger')

// 在url插入的search参数,可以随意,但是必须要
// 例:http://192.168.1.5:8080/?_wx_=1#/
const wxQuery = '_wx_=1'
const isRepeatQuery = location.search.includes(wxQuery)
if (WECHAT_BROWSER && !isRepeatQuery) {
const unit = (location.search && location.search !== '?') ? '&' : '?'
location.search += unit + wxQuery  // 添加_wx_参数,该操作会刷新页面
}

上面的代码之所以要在 hash 前面加一个 ?_wx_=1 参数,为了方便刷新页面给一个标志位判断是否已刷新。参数的 key-value 随意。

来源:https://segmentfault.com/a/1190000018870302

标签:vue,单页面,微信,分享,落地页
0
投稿

猜你喜欢

  • msxml3.dll 错误 '800c0005'终极解决办法

    2009-10-05 18:40:00
  • opencv-python图像增强解读

    2022-10-10 04:29:16
  • 如何在Django中使用聚合的实现示例

    2021-08-02 10:32:30
  • 编写高质量代码的30条黄金守则(首选隐式类型转换)

    2022-07-19 13:03:41
  • Python多维/嵌套字典数据无限遍历的实现

    2023-07-22 16:48:56
  • 对python 多线程中的守护线程与join的用法详解

    2021-08-11 10:56:51
  • python 基于Apscheduler实现定时任务

    2022-03-29 00:53:32
  • C#操作SQLite数据库方法小结(创建,连接,插入,查询,删除等)

    2024-01-23 01:06:29
  • JS和函数式语言的三特性

    2024-04-17 10:10:57
  • python爬虫scrapy框架的梨视频案例解析

    2023-01-16 08:22:19
  • Python创建系统目录的方法

    2023-11-22 11:52:47
  • 重命名SQLServer数据库的方法

    2012-07-11 15:39:37
  • SQL Server中将数据导出为XML和Json方法分享

    2024-01-22 19:04:22
  • 查询字符串中包含特殊字符的问题

    2009-01-09 13:13:00
  • pygame实现方块动画实例讲解

    2022-11-01 13:37:46
  • python中各种路径设置的方法详解

    2022-12-09 23:58:09
  • Python使用captcha库制作带参数输入验证码案例

    2022-01-25 20:26:03
  • 收集的几个Python小技巧分享

    2023-06-14 01:54:01
  • ASP中数据库调用中常见错误的现象和解决

    2007-09-20 13:24:00
  • python实现定时发送邮件到指定邮箱

    2023-05-02 05:04:02
  • asp之家 网络编程 m.aspxhome.com