类型"ImportMeta"上不存在属性"env"(Property 'env' does not exist on type 'ImportMeta')


# tsconfig.json

{
  "compilerOptions": {
    "types": ["vite/client"],
    ...
  }
} 

 

读取环境变量文件.env Files

Vite使用dotenv从你的项目根目录下的以下文件中加载额外的环境变量:

.env.[mode]         # only loaded in specified mode

 

为了防止意外地将env变量泄漏到客户端,只有以VITE_前缀的变量才会暴露给经过VITE_处理的代码。例如:

# .env.production
# 读取 console.log(import.meta.env.VITE_API_URL);
# 读取 console.log(import.meta.env.MAP_KEY);
MAP_KEY = xxxxxxxxxxxxxxx
VITE_API_URL = http://prod.api.com

 只有VITE_API_URL会暴露为import.meta.env。您的客户端源代码的VITE_API_URL,但MAP_KEY不会。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM