如何管理VUE項目中的環境變量
環境變量管理在Vue項目中至關重要,需根據構建工具選擇正確方式。 1. Vue CLI項目使用VUE_APP_前綴的.env文件,通過process.env.VUE_APP_訪問,如.env.production;2. Vite項目使用VITE_前綴,通過import.meta.env.VITE_訪問,如.env.staging;3. 兩者均支持自定義模式加載對應文件,且應將.env.local類文件加入.gitignore;4. 始終避免在前端暴露敏感信息,提供.env.example供參考,並在運行時校驗必要變量,變量為靜態構建時注入,若需動態配置應使用運行時加載方案。
Managing environment variables in a Vue project is essential for handling different configurations across development, testing, and production environments—especially for API endpoints, feature flags, or third-party service keys. Here's how to do it properly depending on your setup (Vue CLI or Vite), which are the two most common Vue scaffolding tools.

1. Using .env
files in Vue CLI
If you're using Vue CLI , it supports environment variables through .env
files. These files are automatically loaded during build and serve processes.
Supported .env
file types:
-
.env
– loaded in all cases -
.env.local
– loaded in all cases, but ignored by git (use for secrets) -
.env.[mode]
– only loaded in specified mode (eg,.env.production
) -
.env.[mode].local
– same as above, but ignored by git
Example files:
# .env.development VUE_APP_API_URL=https://api.dev.example.com VUE_APP_DEBUG=true # .env.production VUE_APP_API_URL=https://api.example.com VUE_APP_DEBUG=false
Rules to follow:
- Only variables prefixed with
VUE_APP_
are embedded into the client bundle. - Access them via
process.env.VUE_APP_YOUR_VAR
in your code.
// In a Vue component or JS file console.log(process.env.VUE_APP_API_URL);
⚠️ Never store sensitive data (like API secrets) in environment variables that end up in the browser. They are embedded into the JS bundle and are visible to anyone.
2. Environment variables in Vite-based Vue projects
If you're using Vite (common in Vue 3 projects created with create-vue
or vite init
), the handling is slightly different.
Vite .env
file rules:
- Variables must be prefixed with
VITE_
to be exposed to the client code. - Files:
.env
,.env.local
,.env.[mode]
(eg,.env.production
)
Example:
# .env.development VITE_API_URL=https://api.dev.example.com VITE_APP_NAME=My App Dev # .env.production VITE_API_URL=https://api.example.com VITE_APP_NAME=My App
Access in code:
console.log(import.meta.env.VITE_API_URL); console.log(import.meta.env.VITE_APP_NAME);
? Vite uses
import.meta.env
instead ofprocess.env
.
3. Using different modes and custom environments
Both Vue CLI and Vite support custom modes.
In Vue CLI:
# Use a custom mode vue-cli-service serve --mode staging
It will load .env.staging
.
In Vite:
# In package.json scripts "scripts": { "serve:staging": "vite --mode staging" }
Then create a vite.config.js
to define the mode and load appropriate .env
files:
export default ({ mode }) => { process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }; return { // your config }; };
4. Best practices
- Never commit secrets : Add
.env.local
and.env.*.local
to.gitignore
. - Provide examples : Include
.env.example
in your repo with placeholder values.# .env.example VUE_APP_API_URL=your_dev_api_url VITE_API_URL=your_dev_api_url
- Validate required variables : Check at runtime if critical variables are defined.
- Use them at build time : Environment variables are static—set at build, not runtime. If you need runtime config, consider a config file fetched on load.
Summary
Tool | Prefix | Access via | File example |
---|---|---|---|
Vue CLI | VUE_APP_
|
process.env.VUE_APP_*
|
.env.production
|
Vite | VITE_
|
import.meta.env.VITE_*
|
.env.staging
|
Choose the right pattern based on your build tool, keep secrets out of the frontend, and use modes to separate environments.
Basically, just name your files correctly, use the right prefix, and access them in code as shown—no extra libraries needed.
以上是如何管理VUE項目中的環境變量的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Stock Market GPT
人工智慧支援投資研究,做出更明智的決策

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

選擇合適的PHP框架需根據項目需求綜合考慮:Laravel適合快速開發,提供EloquentORM和Blade模板引擎,便於數據庫操作和動態表單渲染;Symfony更靈活,適合複雜系統;CodeIgniter輕量,適用於對性能要求較高的簡單應用。 2.確保AI模型準確性需從高質量數據訓練、合理選擇評估指標(如準確率、召回率、F1值)、定期性能評估與模型調優入手,並通過單元測試和集成測試保障代碼質量,同時持續監控輸入數據以防止數據漂移。 3.保護用戶隱私需採取多項措施:對敏感數據進行加密存儲(如AES

Goprovidesbuilt-insupportforhandlingenvironmentvariablesviatheospackage,enablingdeveloperstoread,set,andmanageenvironmentdatasecurelyandefficiently.Toreadavariable,useos.Getenv("KEY"),whichreturnsanemptystringifthekeyisnotset,orcombineos.Lo

1.PHP電商後台主流框架有Laravel(開發快、生態強)、Symfony(企業級、結構穩)、Yii(性能優、適合標準化模塊);2.技術棧需搭配MySQL Redis緩存 RabbitMQ/Kafka消息隊列 Nginx PHP-FPM,並考慮前後端分離;3.高並發架構應分層模塊化、數據庫讀寫分離/分庫分錶、用緩存和CDN加速、異步處理任務、負載均衡與Session共享、逐步微服務化並建立監控告警體系;4.多元變現路徑包括商品差價或平台佣金、站內廣告、SaaS訂閱、定制開發與插件市場、API接

設計一個既實用又能變現的PHPCRM系統,首先要打造包含客戶管理、銷售追踪、自動化流程等核心功能的MVP,並採用模塊化架構(如Laravel)支持後續增值功能擴展;2.通過直觀UX設計(如Vue.js前端)降低使用門檻,讓用戶願意持續付費;3.利用數據分析報告(如銷售漏斗、績效分析)幫助客戶提升決策效率,基礎功能免費、高級報告付費實現變現;4.實施多租戶架構保障數據隔離,為SaaS模式打下基礎,避免後期重構影響商業化;5.變現不僅靠訂閱費,還可通過API開放、定制開發、技術支持及插件市場多元獲益

安裝VueI18n:Vue3使用npminstallvue-i18n@next,Vue2使用npminstallvue-i18n;2.在locales目錄下創建語言文件如en.json和es.json,支持嵌套結構;3.在Vue3中通過createI18n創建實例並在main.js中掛載,Vue2中通過Vue.use(VueI18n)並實例化VueI18n;4.模板中使用{{$t('key')}}插值,Vue3CompositionAPI中使用useI18n的t函數,Vue2OptionsAPI

創建一個主題切換組件,使用複選框綁定isDarkMode狀態並調用toggleTheme函數;2.在onMounted中檢查localStorage和系統偏好設置初始化主題;3.定義applyTheme函數將dark-mode類應用到html元素以切換樣式;4.使用CSS自定義屬性定義亮色和暗色變量,並通過dark-mode類覆蓋默認樣式;5.將ThemeSwitcher組件引入主應用模板中以顯示切換按鈕;6.可選地監聽prefers-color-scheme變化以同步系統主題。該方案利用Vue

環境變量管理在Vue項目中至關重要,需根據構建工具選擇正確方式。 1.VueCLI項目使用VUE_APP_前綴的.env文件,通過process.env.VUE_APP_訪問,如.env.production;2.Vite項目使用VITE_前綴,通過import.meta.env.VITE_訪問,如.env.staging;3.兩者均支持自定義模式加載對應文件,且應將.env.local類文件加入.gitignore;4.始終避免在前端暴露敏感信息,提供.env.example供參考,並在運行時校

創建Modal.vue組件,使用CompositionAPI定義接收modelValue和title的props,並通過emit觸發update:modelValue事件實現v-model雙向綁定;2.在模板中使用slot分發內容,支持默認插槽及具名插槽header和footer;3.通過@click.self實現點擊遮罩層關閉彈窗;4.在父組件中導入Modal並用ref控制顯示隱藏,結合v-model使用;5.可選增強功能包括監聽Escape鍵關閉、添加過渡動畫和焦點鎖定。該模態框組件具有良好
