vue單頁面在微信下只能分享落地頁的解決方案
實際上關鍵詞叫微信pushState只能分享落地頁 更貼切一點
應用場景:
- vue + vue-router
- vue-router使用hash模式(history模式沒試過)
- 不使用微信的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
隨意。