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

# tsconfig.json

{
  "compilerOptions": {
    "types": ["vite/client"],
    ...
  },
 "include: [
   "src/**/*.ts" 
 ] }

 记得把新加的文件加入include配置中

 

读取环境变量文件.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不会。

posted @ 2021-08-02 11:18  Peter_Yang0942  阅读(9369)  评论(0编辑  收藏  举报