0%

vue-cli3.x兼容ie

问题vue项目不兼容ie

要求:ie兼容 >= 9

环境:vue cli3以上版本搭建的项目

Installation

1
2
3
npm install babel/polyfill --save
# or
yarn add babel/polyfill

修改babel.config.js文件

1
2
3
4
5
6
7
8
9
10
11
12
module.exports = {
presets: [
'@vue/app',
// 兼容配置
[
'@babel/preset-env',
{
'useBuiltIns': 'entry'
}
]
]
}

main.js文件(项目入口)

1
2
3
4
// 在顶部引入@babel/polyfill处理兼容,以确保首先加载polyfill
import '@babel/polyfill'

import Vue from 'vue'

重启项目如果没有报错,就已经完成了,如果在编译时出现以下错误,

1
To install them, you can run: npm install --save core-js/modules/es6.array.copy-within core-js/modules/es6.array.fill core-js/modules/es6.array.find core-js/modules/es6.array.find-index core-js/modules/es6.array.from core-js/modules/es6.array.iterator core-js/modules/es6.array.of core-js/modules/es6.array.species core-js/modules/es6.date.to-primitive core-js/modules/es6.function.has-instance core-js/modules/es6.function.name core-js/modules/es6.map core-js/modules/es6.math.acosh core-js/modules/es6.math.asinh core-js/modules/es6.math.atanh core-js/modules/es6.math.cbrt core-js/modules/es6

这是因为最新的 vue-cli 版本,core-js是3.x的版本,而这个版本中,对那些polly补丁包进行了整理,所以,在项目的根目录,yarn add core-js@2.6.9 --save安装这个版本就没问题