国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解vue填坑之解決部分瀏覽器不支持pushState方法

2024-05-06 15:32:47
字體:
來源:轉載
供稿:網友

前端使用vue-router做單頁面路由并開啟history模式時,會碰到一個問題:部分低版本的手機瀏覽器、部分app以及IE9瀏覽器由于不支持pushState方法,會導致頁面加載不出來。 解決這個問題的思路是:

當瀏覽器支持pushState方法時,開啟history模式,不支持則開啟hash模式 對鏈接做判斷,當跳轉的鏈接與路由模式不匹配時,則跳轉至正確的鏈接 nginx對域名下的路徑訪問均重寫向至index.html

以下為具體實現方法:

判斷使用何種路由模式

let isHans = typeof (history.pushState) === 'function';let mode = isHans?'history':'hash';

判斷請求鏈接

每次進入路由時,判斷請求鏈接跳轉的鏈接與路由模式不匹配時,則跳轉至正確的鏈接

router.beforeEach(async (to, from, next) => {  let toPath = to.fullPath,host = 'http://abc.cn';  let url = host + toPath;  let reUrl = url;  if(isHans && url.indexOf(`${host}/#/`) >-1){    reUrl = url.replace(`${host}/#/`,`${host}/car-insurance/`);  }  if(!isHans && url.indexOf(`${host}/#/`) === -1){    reUrl = url.replace(`${host}/car-insurance/`,`${host}/#/`);    reUrl = reUrl.replace(`${host}/`,`${host}/#/`);  }  if(reUrl !== url){    window.location.replace(reUrl);    return  }

配置nginx

server {  listen 80;  listen 443;  server_name abc.cn;  root /data/html;  index index.html index.htm index.json;  access_log off ;  set $isIndex 1;  ##判斷IE6-8  if ($http_user_agent ~* "MSIE [6-8].[0-9]") {    rewrite .* /static/ie8.html break;  }  if ( $request_uri ~* "/(favicon.ico|index.js|root.txt|jd_root.txt)$" ) {   #不跳轉到index.html    set $isIndex 0;  }  if ( $request_uri ~* "/static/" ) {   #不跳轉到index.html    set $isIndex 0;  }  if ($isIndex = 1 ){      set $inIndexJS 0;      rewrite .* /index.html;      break;   }}a

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 宝丰县| 沁源县| 铅山县| 上林县| 巴彦淖尔市| 阳西县| 余庆县| 宝兴县| 通山县| 漳州市| 桃江县| 泰兴市| 崇文区| 宜宾县| 贵德县| 长白| 桐庐县| 崇州市| 榆中县| 德庆县| 张家界市| 吉木萨尔县| 开远市| 揭西县| 肃宁县| 海丰县| 额济纳旗| 武清区| 康平县| 乃东县| 泽普县| 新巴尔虎左旗| 乌鲁木齐县| 克拉玛依市| 锦州市| 栾川县| 千阳县| 驻马店市| 高碑店市| 深圳市| 荆门市|