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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

偂缘   2021-03-29 我要评论
想了解详解vue-cli 构建Vue项目遇到的坑的相关内容吗,偂缘在本文为您仔细讲解vue cli构建项目的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:vue,cli构建项目,vuecli构建项目,用vue,cli构建项目,下面大家一起来学习吧。

前言

使用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。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们