Nuxt3 中使用 Vite 环境变量

11/16/2022 javascriptNuxt3

环境变量和模式 (opens new window) environment-variables-and-private-tokens (opens new window) nuxi build (opens new window)

nuxt3开箱即支持使用.env配置环境变量

// .env
VITE_SOME_KEY=123
1
2
// 使用
console.log(import.meta.env.VITE_SOME_KEY) // 123
1
2

不同的是nuxt3目前不支持多模式自动覆盖,但可以自己手动指定.env.[mode]文件

// .env.staging
VITE_SOME_KEY=123456
1
2
// package.json
"build": "nuxi build",
"build:staging": "nuxi build --dotenv .env.staging",
1
2
3
Last Updated: 9/22/2023, 6:04:11 AM