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

首頁 > 語言 > JavaScript > 正文

詳解Vue-cli3 項(xiàng)目在安卓低版本系統(tǒng)和IE上白屏問題解決

2024-05-06 15:40:44
字體:
供稿:網(wǎng)友

最近遇到一個(gè)問題,用 Vue 開發(fā)的項(xiàng)目在最近兩年新出的安卓手機(jī)上沒問題,在三四年前的舊手機(jī)上出現(xiàn)白屏問題。分析一下應(yīng)該是安卓系統(tǒng)版本的原因,目前已知的是Android 6.0 以上都 OK,6.0 以下就不行了。

低版本安卓系統(tǒng)內(nèi)置的 webview 不支持 ES6 語法等一些新特性,所以報(bào)錯(cuò)。但在手機(jī)上調(diào)試不方便,受一篇文章的啟發(fā), IE 瀏覽器也是同樣的問題,所以可以在 IE 上調(diào)試,一個(gè)調(diào)好了兩個(gè)就都好了。突然發(fā)現(xiàn)萬惡的 IE 還是有點(diǎn)用的…

網(wǎng)上的文章大部分是 Vue-cli 2.x 版本的解決方案,但 Vue-cli 3 跟之前的版本還是有很大差異的,可能是我比較菜,看了 n 篇文章還是不知道怎么配置。經(jīng)過努力,終于梳理出了基于 Vue-cli 3 的項(xiàng)目如何做兼容性配置的步驟:

1. 根目錄下新建 .babelrc 文件

在項(xiàng)目根目錄下新建 .babelrc 文件,跟 package.json 同級(jí)。 將以下代碼復(fù)制到 .babelrc 文件中

{ "presets": ["@babel/preset-env"], "plugins": [  "@babel/plugin-transform-runtime" ]}

2. 修改 babel.config.js

將以下代碼復(fù)制到 babel.config.js 文件中,其中最上面四行是打包時(shí)刪除 console 的配置,如不需要可以刪除。

const plugins = [];if (['production', 'prod'].includes(process.env.NODE_ENV)) { plugins.push("transform-remove-console")}module.exports = { presets: [  [   "@vue/app",   {    "useBuiltIns": "entry",    polyfills: [     'es6.promise',     'es6.symbol'    ]   }  ] ], plugins: plugins};

3. 修改 vue.config.js

用 vue-cli 3 新建項(xiàng)目時(shí),默認(rèn)是沒有這個(gè)配置文件的,沒有則在項(xiàng)目根目錄下新建一個(gè) vue.config.js ,也是跟 package.json 同級(jí)。

解決白屏問題需要添加的代碼:

module.exports = { transpileDependencies: ['webpack-dev-server/client'], chainWebpack: config => {  config.entry.app = ['babel-polyfill', './src/main.js']; }}

4. 修改 main.js 文件

找到 項(xiàng)目根目錄/src/main.js ,添加以下代碼

import '@babel/polyfill';import Es6Promise from 'es6-promise'Es6Promise.polyfill()

5. 安裝依賴

在根目錄下執(zhí)行以下語句。如果在第二步不需要配置生產(chǎn)環(huán)境刪除 console 可以不要最后一個(gè) babel-plugin-transform-remove-console 。

npm install --save-dev @babel/core @babel/plugin-transform-runtime @babel/preset-env es6-promise babel-polyfill babel-plugin-transform-remove-console

以上五步配置完就可以解決 Vue 項(xiàng)目在低版本安卓系統(tǒng)和 IE 瀏覽器下顯示空白的問題了。

附完整的 vue.config.js

const path = require('path')const resolve = dir => path.resolve(__dirname, dir)const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;const CompressionWebpackPlugin = require('compression-webpack-plugin');const productionGzipExtensions = //.(js|css|json|txt|html|ico|svg)(/?.*)?$/i;module.exports = { transpileDependencies: ['webpack-dev-server/client'], // 基本路徑 // baseUrl: './', publicPath: './', // 輸出文件目錄 outputDir: 'dist', // eslint-loader 是否在保存的時(shí)候檢查 lintOnSave: false, assetsDir: '', // 相對(duì)于outputDir的靜態(tài)資源(js、css、img、fonts)目錄 runtimeCompiler: true, // 是否使用包含運(yùn)行時(shí)編譯器的 Vue 構(gòu)建版本 // 生產(chǎn)環(huán)境是否生成 sourceMap 文件 productionSourceMap: false, chainWebpack: config => {  config.entry.app = ['babel-polyfill', './src/main.js'];  // 修復(fù)HMR  config.resolve.symlinks(true);  //修復(fù) Lazy loading routes Error  config.plugin('html').tap(args => {   args[0].chunksSortMode = 'none';   return args;  });  // 添加別名  config.resolve.alias   .set('@', resolve('src'))   .set('assets', resolve('src/assets'))   .set('components', resolve('src/components'))   .set('layout', resolve('src/layout'))   .set('base', resolve('src/base'))   .set('static', resolve('src/static'));  //壓縮圖片  config.module   .rule("images")   .use("image-webpack-loader")   .loader("image-webpack-loader")   .options({    mozjpeg: {progressive: true, quality: 65},    optipng: {enabled: false},    pngquant: {quality: "65-90", speed: 4},    gifsicle: {interlaced: false},    webp: {quality: 75}   });  // 打包分析  if (process.env.IS_ANALYZ) {   config.plugin('webpack-report')    .use(BundleAnalyzerPlugin, [{     analyzerMode: 'static',    }]);  } }, configureWebpack: config => {  if (IS_PROD) {   const plugins = [];   //開啟 gzip 壓縮   plugins.push(    new CompressionWebpackPlugin({     filename: '[path].gz[query]',     algorithm: 'gzip',     test: productionGzipExtensions,     threshold: 10240,     minRatio: 0.8    })   );   config.plugins = [    ...config.plugins,    ...plugins   ];  } }, // css相關(guān)配置 css: {  extract: true,  sourceMap: false,  loaderOptions: {},  modules: false }, parallel: require('os').cpus().length > 1, pwa: {}, devServer: {  open: process.platform === 'darwin',  host: '0.0.0.0',  port: 8080,  https: false,  hotOnly: false,  proxy: null, // 設(shè)置代理  before: app => {  } }, // 第三方插件配置 pluginOptions: {}};            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 丽江市| 津市市| 锦屏县| 大厂| 霞浦县| 乌鲁木齐县| 安福县| 鸡泽县| 文昌市| 深州市| 蒙自县| 尼勒克县| 买车| 华安县| 普洱| 芜湖市| 漯河市| 如皋市| 五指山市| 盐源县| 寻乌县| 阿巴嘎旗| 巴彦县| 彰化市| 石屏县| 九龙城区| 海口市| 安国市| 博兴县| 安宁市| 禄丰县| 清水县| 崇文区| 朔州市| 应城市| 舒兰市| 天水市| 平顶山市| 荃湾区| 科技| 柘城县|