详解vue-cli 构建Vue项目遇到的坑

2019-11-16,,,

前言

使用Vue做管理系统和WebApp做过三四个项目了,期间遇到很多坑,再次一一列举,并给出解决方案,方便日后更方便使用,也为他人提供一些解决方案。

问题表象和解决方案

1、编译后js、css等相对路径和绝对路径。

config/inde.js文件
 build: {
  env: require('./prod.env'),
  index: path.resolve(__dirname, '../dist/index.html'),
  assetsRoot: path.resolve(__dirname, '../dist'),
  assetsSubDirectory: 'static',
  assetsPublicPath: './', // "./"表示相对路径 编译结果 <link href=./static/css/app.518fd3471fd03bfce5524de6e934771c.css rel=stylesheet>
  productionSourceMap: true,
  // Gzip off by default as many popular static hosts such as
  // Surge or Netlify already gzip all static assets for you.
  // Before setting to `true`, make sure to:
  // npm install --save-dev compression-webpack-plugin
  productionGzip: false,
  productionGzipExtensions: ['js', 'css'],
  // Run the build command with an extra argument to
  // View the bundle analyzer report after build finishes:
  // `npm run build --report`
  // Set to `true` or `false` to always turn it on or off
  bundleAnalyzerReport: process.env.npm_config_report
 },

2、CSS中图片相对路径编译后,路径不对。

//同常路径表现为 ./static/img/static/img/*.jpg (定义为imgurl)
解决方案 修改build/untils.js
//替换相应代码
if (options.extract) {
   return ExtractTextPlugin.extract({
    use: loaders,
    publicPath: '../../', //注意: 此处根据路径, 自动更改 ,(imgurl='static/img/*.jpg')
    fallback: 'vue-style-loader'
   })
  } else {
   return ['vue-style-loader'].concat(loaders)
  }

3、使用代理,解决跨域请求。

vue-cli已经集成 http-proxy-middleware插件

github:https://github.com/chimurai/http-proxy-middleware

proxyTable: {
   '/dianmi_service': {
    target: 'https://****.com.cn',
    changeOrigin: true
   },
   '/brand_service': {
    target: 'https://*****.com.cn',
    changeOrigin: true
   }
  },

其中 target的值一定不可以包含工程名,否则可能会出现服务器Session问题

4、用Vue做页面,利用phonegap编译android APP时,兼容问题。

cordova android 4.0 以下的默认webView,不支持ES6,会导致一些列问题。

好在,cordova android允许我们自定义自己的webView引擎。那么我们就想到了 腾讯浏览服务X5内核。

然而有大神基于此内核编写了cordova 插件

githup:https://github.com/offbye/cordova-plugin-x5engine-webview

使用此插件很好的解决 低版本安卓不支持Vue。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持北冥有鱼。

您可能感兴趣的文章:

  • vue-cli构建vue项目的步骤详解
  • 使用Vue-cli 3.0搭建Vue项目的方法
  • 详解使用vue-cli脚手架初始化Vue项目下的项目结构
  • vue-cli如何快速构建vue项目
  • 详解用vue-cli来搭建vue项目和webpack
  • 图文讲解用vue-cli脚手架创建vue项目步骤

《详解vue-cli 构建Vue项目遇到的坑.doc》

下载本文的Word格式文档,以方便收藏与打印。