Nuxt3-环境变量配置
Nuxt3 环境变量
1、创建 .env 文件
// .env.development 本地环境 NUXT_NODE_ENV = development NUXT_BASE_URL = http://yfnimg.bestsmell.cn:10011
// .env.test 预发环境 NUXT_NODE_ENV = pre NUXT_BASE_URL = https://pre-api.yfn.com
// .env.production 生产环境 NUXT_NODE_ENV = production NUXT_BASE_URL = https://api.yfn.com
2、配置运行、编译环境 package.json
// --dotenv 指定 env 文件 "dev": "nuxt dev --dotenv .env.development --exec", "pre": "nuxt dev --dotenv .env.test --exec", "pro": "nuxt dev --dotenv .env.production --exec", "build": "nuxt build --dotenv .env.development --exec", "build:pre": "nuxt build --dotenv .env.test --exec", "build:pro": "nuxt build --dotenv .env.production --exec",
3、nuxt.config.ts 内配置
4、本地运行
// 运行测试环境 npm run dev or yarn dev // 运行预发环境 npm run pre or yarn pre // 运行生产环境 npm run pro or yarn pro
5、页面内使用,示例为运行 预发环境:yarn pre