vue cli3備忘

  • 多頁面或者多項目配置
    projects.js
const config = {
  web: {
    pages: {
      index: {
        entry: 'src/main.js',
        template: 'public/index.html',
        filename: 'index.html',
        title: 'web'
      },
      api: {
        entry: 'src/pages/api/main.js',
        template: 'public/api.html',
        filename: 'api.html',
        title: 'api 測試'
      }
    },
    devServer: {
      port: 8080, // 端口地址
      open: true,
      overlay: {
        warnings: false,
        errors: true
      },
      historyApiFallback: { // 路由history模式必須配置
        verbose: true,
        rewrites: [
          { from: /^\/index\/.*$/, to: '/index.html' },
          { from: /^\/api\/.*$/, to: '/api.html' }
        ]
      }
    }
  },
  api: {
    pages: {
      index: {
        entry: 'src/projects/api/main.js',
        template: 'public/index.html',
        filename: 'index.html'
      }
    },
    devServer: {
      port: 8081, // 端口地址
      open: true,
      overlay: {
        warnings: false,
        errors: true
      }
    }
  }
}
module.exports = config

  • 開啟gzip
    先安裝打包插件compression-webpack-plugin:npm install --save-dev compression-webpack-plugin,再修改vue.config.js文件
'use strict'

const projects = require('./config/projects.js') // 引入子系統(tǒng)運行打包配置
const CompressionPlugin = require('compression-webpack-plugin')
let projectName = process.env.PROJECT_NAME // 獲取package.json中scripts配置的變量
console.log(projectName)
module.exports = {
  ...projects[projectName],
  publicPath: '/',
  // 輸出文件目錄
  outputDir: 'dist/' + projectName + '/',
  publicPath: '/',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      return {
        plugins: [
          new CompressionPlugin({
            test: /\.js$|\.html$|\.css/,
            threshold: 10240,
            deleteOriginalAssets: false
          })
        ]
      }
    }
  }
}

  • el-menu導航重復點擊可能控制臺會報[NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}]

解決:
方案1
使用低版本,vue-router版本到3.0.7以下
方案2
禁止全局路由錯誤打印

import Router from 'vue-router'
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

方案3(推薦)
給每個router.push增加回調(diào)函數(shù)。

router.push('/index').catch(err => {err})
  • 正式環(huán)境移除console

安裝依賴包:npm install babel-plugin-transform-remove-console --save-dev
配置:Vue項目根目錄 babel.config.js 文件

const plugins = []
if (process.env.NODE_ENV === 'production') {
  plugins.push('transform-remove-console');
}
module.exports = {
  presets: [
    '@vue/app'
  ],
  plugins: plugins
}
  • 跨域
    vue.config.js配置
proxy: {
      '/proxy': {
        target: 'http://192.168.1.123:8080/xxx/',  //真實的后臺接口
        changOrigin: true,  //允許跨域
        pathRewrite: {
          '^/proxy': '' 
        }
      },
devServer: {
    host: '0.0.0.0',
    port: port,
    open: true,
    proxy: {
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      [process.env.VUE_APP_BASE_API]: {
        target: `http://192.168.1.50:8080`,
        changeOrigin: true,
        pathRewrite: {
          ['^' + process.env.VUE_APP_BASE_API]: ''
        }
      },
      // '/dev-api': {
      //   target: 'http://192.168.1.50:8080',  //真實的后臺接口
      //   changOrigin: true,  //允許跨域
      //   pathRewrite: {
      //     '^/dev-api': '' 
      //   }
      // }
    },
    disableHostCheck: true
  },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容