###### tags: `Vite` `Vue` # Vite + Vue3 環境變量解決方法 在 vite.config.js 中 ```javascript= import { loadEnv } from 'vite' ``` 將原本的 ```javascript= export default defineConfig({ // ... }) ``` 修改為 ```javascript= export default ({ mode }) => { return defineConfig({ }) } ``` 環境變數取得語法: loadEnv(mode, process.cwd()).VITE_ENV_VARIABLE_NAME 例如修改 base url,變數名定為 VITE_BASE_URL: ```javascript= export default ({ mode }) => { return defineConfig({ base:`/${loadEnv(mode, process.cwd()).VITE_BASE_URL}` }) }; ``` 須要建立 .env.production 及 .env.development 在專案根目錄,並定義相關變數,否則會有不可編譯之錯誤發生。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up