亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解項(xiàng)目升級到vue-cli3的正確姿勢

 更新時(shí)間:2019年01月28日 09:38:13   作者:前端勸退師  
這篇文章主要介紹了詳解項(xiàng)目升級到vue-cli3的正確姿勢,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

一. 原以為升級vue-cli3的路線是這樣的:

創(chuàng)建vue-cli3項(xiàng)目,按原有項(xiàng)目的配置選好各項(xiàng)配置

遷移目錄

src->src
static->public

對比新舊 package.json ,然后 yarn install ,完畢。

然鵝... 運(yùn)行項(xiàng)目,報(bào)錯(cuò) You are using the runtime-only build of Vue......

然后去查了下舊項(xiàng)目的相關(guān)字眼文件:

噢,原來是vue-cli3的webpack相關(guān)文件都得自己寫。于是乎根據(jù)官網(wǎng)的指引,在根目錄創(chuàng)建了 vue.config.js

此時(shí)粗略配置:

 chainWebpack: config => {
 config.module
  .rule('vue')
  .use('vue-loader')
  .loader('vue-loader')
  .tap(options => {
  options.compilerOptions.preserveWhitespace = false
  return options
  })
 config.resolve.alias
  .set('vue$', 'vue/dist/vue.esm.js')
  .set('@', resolve('src'))
 }

二. 此時(shí)勉強(qiáng)能跑起來,但后續(xù)遇到了這些坑:

#1 public 靜態(tài)資源不加載

```
 const CopyWebpackPlugin = require('copy-webpack-plugin')
 // ....
 // 確保靜態(tài)資源
 config.resolve.extensions = ['.js', '.vue', '.json', '.css']
 config.plugins.push(
 new CopyWebpackPlugin([{ from: 'public/', to: 'public' }]),
)
```

#2 Chrome 查看樣式時(shí)無法找到源文件

原因: vue-cli3 里默認(rèn)關(guān)閉 sourceMap,樣式都會(huì)被打包到首頁。 解決: 需要自己配置打開

 // 讓樣式找到源
 css: {
 sourceMap: true
 },

#3 生產(chǎn)環(huán)境的 debugerconsole 無法通過 uglifyjs-webpack-pluginuglify-es 剔除

原因:不支持 es6 , 需要配置 babel ( uglify-es 按配置填會(huì)顯示不存在選項(xiàng))

解決:插件terser

```
const TerserPlugin = require('terser-webpack-plugin')
if (process.env.NODE_ENV === 'production') {
 // 為生產(chǎn)環(huán)境修改配置...
 new TerserPlugin({
 cache: true,
 parallel: true,
 sourceMap: true, // Must be set to true if using source-maps in production
 terserOptions: {
  compress: {
  drop_console: true,
  drop_debugger: true
  }
 }
 })
} else {
 // 為開發(fā)環(huán)境修改配置...
}
```

#4 無法在 config 目錄下配置不同環(huán)境的 API_URL ,用于跨域請求

原因: vue-cli3 中需要遵循變量規(guī)則,使用 VUE_APP 前綴

官方規(guī)則: 在客戶端側(cè)代碼中使用環(huán)境變量

解決:于是你需要?jiǎng)?chuàng)建如下幾個(gè)文件:

.local 也可以加在指定模式的環(huán)境文件上,比如 .env.development.local 將會(huì)在 development 模式下被載入,且被 git 忽略。

文件內(nèi)容:

// env.development.local
NODE_ENV = development
VUE_APP_URL = http://xxx.x.xxx/

#5 vue-cli代理轉(zhuǎn)發(fā)控制臺(tái)反復(fù)打印 "WebSocket connection to'ws://localhost..."

解決方法:

vue.config.js 中配置 devServer.proxywsfalse

結(jié)合上述兩步,相對應(yīng)的 vue.config.js ,需要這么寫:

const env = process.env.NODE_ENV
let target = process.env.VUE_APP_URL

const devProxy = ['/api', '/'] // 代理
// 生成代理配置對象
let proxyObj = {};
devProxy.forEach((value, index) => {
 proxyObj[value] = {
 ws: false,
 target: target,
 // 開啟代理:在本地會(huì)創(chuàng)建一個(gè)虛擬服務(wù)端,然后發(fā)送請求的數(shù)據(jù),并同時(shí)接收請求的數(shù)據(jù),這樣服務(wù)端和服務(wù)端進(jìn)行數(shù)據(jù)的交互就不會(huì)有跨域問題
 changeOrigin: true,
 pathRewrite: {
  [`^${value}`]: value
 }
 };
})
// ....
devServer: {
 open: true,
 host: 'localhost',
 port: 8080,
 proxy: proxyObj
 }

最后貼上我的 vue.config.js

const CopyWebpackPlugin = require('copy-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')

const path = require('path')
const env = process.env.NODE_ENV
let target = process.env.VUE_APP_URL

const devProxy = ['/api', '/'] // 代理

// 生成代理配置對象
let proxyObj = {};
devProxy.forEach((value, index) => {
 proxyObj[value] = {
 ws: false,
 target: target,
 // 開啟代理:在本地會(huì)創(chuàng)建一個(gè)虛擬服務(wù)端,然后發(fā)送請求的數(shù)據(jù),并同時(shí)接收請求的數(shù)據(jù),這樣服務(wù)端和服務(wù)端進(jìn)行數(shù)據(jù)的交互就不會(huì)有跨域問題
 changeOrigin: true,
 pathRewrite: {
  [`^${value}`]: value
 }
 };
})

function resolve (dir) {
 return path.join(__dirname, dir)
}

module.exports = {
 publicPath: '/',
 // 讓樣式找到源
 css: {
 sourceMap: true
 },
 configureWebpack: config => {
 // 確保靜態(tài)資源
 config.resolve.extensions = ['.js', '.vue', '.json', '.css']
 config.plugins.push(
  new CopyWebpackPlugin([{ from: 'public/', to: 'public' }]),
 )
 if (process.env.NODE_ENV === 'production') {
  // 為生產(chǎn)環(huán)境修改配置...
  new TerserPlugin({
  cache: true,
  parallel: true,
  sourceMap: true, // Must be set to true if using source-maps in production
  terserOptions: {
   compress: {
   drop_console: true,
   drop_debugger: true
   }
  }
  })
 } else {
  // 為開發(fā)環(huán)境修改配置...
 }

 },
 chainWebpack: config => {
 config.module
  .rule('vue')
  .use('vue-loader')
  .loader('vue-loader')
  .tap(options => {
  options.compilerOptions.preserveWhitespace = false
  return options
  })
 config.resolve.alias
  .set('vue$', 'vue/dist/vue.esm.js')
  .set('@', resolve('src'))
 },
 devServer: {
 open: true,
 host: 'localhost',
 port: 8080,
 proxy: proxyObj
 }
}

三. Eslint相關(guān)報(bào)錯(cuò)及配置

module.exports = {
 root: true,
 env: {
 node: true
 },
 'extends': [
 'plugin:vue/essential',
 '@vue/standard'
 ],
 rules: {
 'generator-star-spacing': 'off',
 'object-curly-spacing': 'off',
 // 最常出現(xiàn)的錯(cuò)誤
 'no-unused-vars': 'off',
 // 最常出現(xiàn)的錯(cuò)誤
 "vue/no-use-v-if-with-v-for": ["error", {
  "allowUsingIterationVar": true
 }],
 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
 },
 parserOptions: {
 parser: 'babel-eslint'
 }
}

最后的最后,跑個(gè)項(xiàng)目

yarn serve

yarn build

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論