1. 卸載vue-cli 2.0
npm uninstall -g vue-cli
2. 安裝vue-cli 3.0
npm install @vue/cli
3. 創建項目
npm create <項目名>
4. 如果創建出現如下錯誤: Unexpected end of JSON input while parsing near '...ownload/webpack-0.3.2',則繼續下面的步驟。
5. 執行下面兩個操作
(1)npm install --registry=https://registry.npm.taobao.org
(2)npm cache clean --force
(3)刪除原來創建的項目文件,重新執行第3條命令 npm create <項目名>
6. 執行npm run serve,即可啟動服務,可通過瀏覽器打開頁面鏈接
成功創建的項目目錄結構如下:
7. 可根據需要在項目根目錄添加vue.config.js配置文件,並輸入如下內容:
1 module.exports = { 2 // 基本路徑 3 publicPath: '/', 4 // 輸出文件目錄 5 outputDir: 'dist', 6 // 用於嵌套生成的靜態資產(js,css,img,fonts)的目錄。 7 assetsDir: '', 8 // 以多頁模式構建應用程序。 9 pages: undefined, 10 // eslint-loader 是否在保存的時候檢查 11 lintOnSave: true, 12 // 是否使用包含運行時編譯器的Vue核心的構建。 13 runtimeCompiler: false, 14 // 默認情況下babel-loader忽略其中的所有文件node_modules。 15 transpileDependencies: [], 16 // 生產環境sourceMap 17 productionSourceMap: true, 18 // webpack配置 19 configureWebpack: () => {}, 20 chainWebpack: () => {}, 21 // css相關配置 22 css: { 23 // 啟用 CSS modules 24 requireModuleExtension: false, 25 // 是否使用css分離插件 26 extract: true, 27 // 開啟 CSS source maps? 28 sourceMap: false, 29 // css預設器配置項 30 loaderOptions: {}, 31 }, 32 // webpack-dev-server 相關配置 33 devServer: { 34 host: '0.0.0.0', 35 port: 8080, 36 https: false, 37 hotOnly: false, 38 proxy: null, // 設置代理 39 before: app => {} 40 }, 41 // enabled by default if the machine has more than 1 cores 42 parallel: require('os').cpus().length > 1, 43 // PWA 插件相關配置 44 pwa: {}, 45 // 第三方插件配置 46 pluginOptions: { 47 // ... 48 } 49 }