uni-qnc-tob/docs/环境变量配置说明.md
2025-03-14 03:40:55 +08:00

54 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 环境变量配置说明
## 环境变量文件
本项目使用 Vite 的环境变量功能,支持以下环境变量文件:
- `.env`:所有环境都会加载的默认变量
- `.env.development`:开发环境变量(`npm run dev` 时加载)
- `.env.production`:生产环境变量(`npm run build` 时加载)
## 已配置的环境变量
| 变量名 | 说明 | 示例值 |
|--------|------|--------|
| VITE_APP_BASE_URL | API 基础URL | https://www.quannengcha.com |
| VITE_APP_DEBUG | 调试模式 | true/false |
## 在项目中使用环境变量
### 在 Vue 组件中使用
```vue
<script setup>
// 使用 import.meta.env 访问环境变量
const baseUrl = import.meta.env.VITE_APP_BASE_URL
const isDebug = import.meta.env.VITE_APP_DEBUG === 'true'
</script>
```
### 在 JS/TS 文件中使用
```js
// 使用 import.meta.env 访问环境变量
const baseUrl = import.meta.env.VITE_APP_BASE_URL
const isDebug = import.meta.env.VITE_APP_DEBUG === 'true'
// 获取当前环境模式
const mode = import.meta.env.MODE
```
## 获取当前环境
```js
// 获取当前环境模式development、production等
const mode = import.meta.env.MODE
```
## 注意事项
1. 所有环境变量必须以 `VITE_` 开头才能在客户端代码中访问
2. 环境变量默认为字符串类型,需要自行转换为其他类型(如布尔值)
3. 修改环境变量后需要重启开发服务器才能生效
4. 不要在环境变量文件中存储敏感信息如API密钥等