在项目根目录下创建 .env.development
文件用于开发环境,创建 .env.production
文件用于生产环境。
在 .env.development
文件中设置开发环境下的环境变量,例如:
VITE_API_BASE_URL=http://localhost:8000
在 .env.production
文件中设置生产环境下的环境变量,例如:
VITE_API_BASE_URL=https://api.example.com
在封装的 HTTP 请求库中使用 import.meta.env.VITE_API_BASE_URL
来获取对应环境文件中的地址变量:
import _axios from 'axios'
const baseURL = import.meta.env.VITE_API_BASE_URL
const axios = _axios.create({
baseURL: baseURL,
// ...
})