初次接觸vue3,有什么不足之處,多多探討<~>
首先用vite創建初始vue項目后,會生成一個默認的vite.config.js文件,如下
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue()] })
顯然不能滿足需求
於是我們可以自行添加大多數配置,修改如下
1 import vue from '@vitejs/plugin-vue' 2 const path = require('path') 3 // vite.config.js # or vite.config.ts 4 console.log(path.resolve(__dirname, './src')) 5 module.exports = { 6 plugins: [vue()], 7 "resolve.alias": { 8 // 鍵必須以斜線開始和結束 9 '/@/': path.resolve(__dirname, './src') 10 }, 11 hostname: '127.0.0.1', 12 port: 3000, 13 // 是否自動在瀏覽器打開 14 open: true, 15 // 是否開啟 https 16 https: false, 17 // 服務端渲染 18 ssr: false, 19 /** 20 * 在生產中服務時的基本公共路徑。 21 * @default '/' 22 */ 23 base: './', 24 /** 25 * 與“根”相關的目錄,構建輸出將放在其中。如果目錄存在,它將在構建之前被刪除。 26 * @default 'dist' 27 */ 28 outDir: 'dist', 29 // 反向代理,此處應該特別注意,網上很多教程是直接設置proxy,並沒有向官網那樣添加 server,可能會導致失敗,vite官網:https://vitejs.dev/guide/features.html#async-chunk-loading-optimization
30 server:{ 31 proxy: { 32 '/api': { 33 target: 'https://api.pingping6.com/tools/baidutop/', 34 changeOrigin: true, 35 rewrite: path => path.replace(/^\/api/, '') 36 } 37 } 38 } 39 40 }
大致如上,主要是設置代理的時候需要特別注意