原文:
vue+axios+vue-router兼容ie9
h5 的replaceState 兼容到低版本IE浏览器
vue 项目不支持ie8 提示问题
IE兼容vue解决方案

一、vue+axios+vue-router兼容ie9(h5 的replaceState 兼容到低版本IE浏览器)

首先感谢大佬:https://github.com/devote/HTML5-History-API

从 GitHub上下载下来 history.js ,或者 压缩版本的min.js ,建议压缩版本,体积小。

放到路径引入一下:

引入成功这个js ,就可以了。

正常用 history的属性即可。因为,他的js,已经把 各种方法在不兼容的浏览器中又重新写了一遍。

不得不说 ,真大佬啊,唉 我努力,以后自己也写一些库

加油~

二、h5 的replaceState 兼容到低版本IE浏览器(vue+axios+vue-router兼容ie9)

前序

项目突然要求兼容ie9…可是用到的技术栈对ie9似乎不是很友好,综合上面的参考的文章,需要添加以下内容:

1.ES6兼容

ES6兼容的话,需要安装特定的模块,执行 命令npm isntall babel-polyfill,然后在文件配置:
兼容ie9什么的 - 图1
babel-polyfill配置

2.vue-router的history模式兼容

因为vue-router对于ie9默认是退回到hash模式的,所以在new router的时候,需要设置fallback:false(默认值为true)

fallback:当浏览器不支持 history.pushState 控制路由是否应该回退到 hash 模式。默认值为 true。 在 IE9 中,设置为 false 会使得每个 router-link 导航都触发整页刷新。它可用于工作在 IE9 下的服务端渲染应用,因为一个 hash 模式的 URL 并不支持服务端渲染。 配置示例:

  1. const router = new Router({
  2. fallback: false,
  3. base: "/test/",
  4. mode: "history",
  5. routes: []
  6. })

3跨域

尽管后端已经配置了CORS,但是在ie9情况下,CORS无效,所以需要nginx设置反向代理.
参考:vue项目部署方式:tomcat部署和nginx部署

4设置了nginx反向代理仍然无法获取response.data

设置了反向代理,正常发出和获取响应,但是response.data却为空值,响应值被存到response.request.responseText中.这时候要利用axios.interceptors.response适当添加兼容代码:

  1. Axios.interceptors.response.use(
  2. function(response) {
  3. //判断是否有是ie9
  4. if (isIE9()) {
  5. //特殊处理response
  6. if (response.status == 200 && response.request) {
  7. if (
  8. response.request.responseType === "json" &&
  9. response.request.responseText
  10. ) {
  11. response.data = JSON.parse(response.request.responseText);
  12. console.log("repsonse", response);
  13. }
  14. }
  15. }
  16. return response;
  17. },
  18. function(error) {
  19. if (error.response) {
  20. // 对响应错误做点什么
  21. console.log("error", error);
  22. return Promise.reject(error);
  23. }
  24. );
  25. function isIE9() {
  26. if (
  27. navigator.appName == "Microsoft Internet Explorer" &&
  28. parseInt(
  29. navigator.appVersion
  30. .split(";")[1]
  31. .replace(/[ ]/g, "")
  32. .replace("MSIE", "")
  33. ) <= 9
  34. ) {
  35. return true;
  36. }
  37. return false;
  38. }

三、vue 项目不支持ie8 提示问题

在vue index.html文件中
var ie=IEVersion();
console.log(ie)
if(ie<=8){
alert(“浏览器版本过低,请升级浏览器”)
}

四、IE兼容vue解决方案(详细见原文)

1、

  1. npm install babel-polyfill -S

2、

  1. npm install es6-promise -S

3、
@babel/plugin-transform-runtime

  1. plugins": ["transform-runtime","transform-vue-jsx",