[2018-07-31更新]: vue-cli 3.0正式版的中文文檔已經出來了,詳細的可以參考:https://cli.vuejs.org/zh/guide/
一、安裝Vue和創建項目:
# 安裝 npm install -g @vue/cli
注:快捷鍵win+R,輸入cmd並運行,默認C盤,可設置對應的磁盤,如F盤, 輸入cd\到C盤更目錄,再輸入f:進入F盤,最后就是自己自定義的文件夾cd vue # 查看已安裝版本 vue --version 或者 vue -V # 卸載 npm uninstall @vue/cli -g # 新建項目 vue create my-project
注:創建自定義項目(my-porject)的時候,可自定義設置項目相關配置,可以End默認,如果設置了的可以在vue.config.js配置文件中再次修改。 # 項目啟動 npm run serve # 打包 npm run build
二、Vue相關配置--vue.config.js
Vue-cli3.0的配置不同於之前版本,大大的簡化了開發者工作,更人性化,只有一個配置文件-vue.config.js。
常用配置:1、樣式預處理less,sass,公共樣式文件設置
2、端口設置和熱加載
3、第三方插件
vue.config.js --- 基礎模版
module.exports = { baseUrl: process.env.NODE_ENV === 'production' ? '//your_url' : '/', outputDir: 'dist', //生成的生產環境構建文件的目錄 assetsDir: 'static', // 放置生成的靜態資源 (js、css、img、fonts) 的 (相對於outputDir
的) 目錄。
indexPath: 'index.html',
//指定生成的
index.html
的輸出路徑 (相對於 outputDir
)。也可以是一個絕對路徑。
filenameHashing: true, // When building in multi-pages mode, the webpack config will contain different plugins // (there will be multiple instances of html-webpack-plugin and preload-webpack-plugin). // Make sure to run vue inspect if you are trying to modify the options for those plugins.
// 在 multi-page 多頁面模式下構建應用。每個“page”應該有一個對應的 JavaScript 入口文件,多頁面配置(web,h5等),當前只有一個單頁面,可以不用配置 pages: {
index: { // entry for the pages entry: 'src/pages/index/index.js', // the source template template: 'src/pages/index/index.html', // output as dist/index.html filename: 'index.html', // when using title option, // template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title> title: '首頁', // chunks to include on this pages, by default includes // extracted common chunks and vendor chunks. chunks: ['chunk-vendors', 'chunk-common', 'index'] } // when using the entry-only string format, // template is inferred to be `public/subpage.html` // and falls back to `public/index.html` if not found. // Output filename is inferred to be `subpage.html`. // subpage: '' }, // eslint-loader 是否在保存的時候檢查 lintOnSave: true, // 是否使用包含運行時編譯器的Vue核心的構建 runtimeCompiler: false, // 默認情況下 babel-loader 忽略其中的所有文件 node_modules transpileDependencies: [], // 生產環境 sourceMap productionSourceMap: false, // cors 相關 https://jakearchibald.com/2017/es-modules-in-browsers/#always-cors // corsUseCredentials: false, // webpack 配置,鍵值對象時會合並配置,為方法時會改寫配置 // https://cli.vuejs.org/guide/webpack.html#simple-configuration configureWebpack: (config) => {
resolve: {
alias: {
'@': resolve('src')
}
},
plugins: [
// webpack 配置jq, 配置目錄別名
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
})
]
// webpack 鏈接 API,用於生成和修改 webapck 配置 // https://github.com/mozilla-neutrino/webpack-chain chainWebpack: (config) => { // 因為是多頁面,所以取消 chunks,每個頁面只對應一個單獨的 JS / CSS config.optimization .splitChunks({ cacheGroups: {} }); // 'src/lib' 目錄下為外部庫文件,不參與 eslint 檢測 config.module .rule('eslint') // 不想校驗可以注釋掉
.use('vue-loader')
.loader('vue-loader') }, // 配置高於chainWebpack中關於 css loader 的配置 css: { // 是否開啟支持 foo.module.css 樣式 modules: false, // 是否使用 css 分離插件 ExtractTextPlugin,采用獨立樣式文件載入,不采用 <style> 方式內聯至 html 文件中 extract: true, // 是否構建樣式地圖,false 將提高構建速度 sourceMap: false, // css預設器配置項 loaderOptions: {
modules: false,
extract: true,
sourceMap: false,
css: { // options here will be passed to css-loader
data: `@import "@/assets/sass/base.scss";` //導入公共樣式文件
}, postcss: { // options here will be passed to postcss-loader } } }, // All options for webpack-dev-server are supported // https://webpack.js.org/configuration/dev-server/ devServer: { open: true, host: '127.0.0.1', port: 3000, // 如果端口沖突,配置端口號 https: false, hotOnly: false, proxy: null, before: app => { } }, // 構建時開啟多進程處理 babel 編譯 parallel: require('os').cpus().length > 1, // https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa pwa: {}, // 第三方插件配置 pluginOptions: {} };
三、一些報錯的坑
1、npm run dev 運行報錯
解決方案:該問題是因為腳手架工具默認監聽的是8080端口,此時是8080端口被占用情況導致的,可以需選擇殺死端口,可以自定義端口,如:3000, 9000。
2、Media query expression must begin with '(' 編譯時報錯
解決方案:scss編譯的問題,style中scss中使用{},lang='scss',在應用公共樣式文件base.scss,
data: `@import "@/assets/sass/base.scss";`后面一定要帶上分號‘;’