Skip to content

Commit

Permalink
i18n(zh-cn): update environment-variables.mdx (#4111)
Browse files Browse the repository at this point in the history
* i18n(zh-cn): update environment-variables.mdx

* Update src/content/docs/zh-cn/guides/environment-variables.mdx

Co-authored-by: 李瑞丰 <[email protected]>

---------

Co-authored-by: Yan Thomas <[email protected]>
Co-authored-by: 李瑞丰 <[email protected]>
  • Loading branch information
3 people committed Aug 8, 2023
1 parent 888bc9d commit 50b41a1
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/content/docs/zh-cn/guides/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ PUBLIC_POKEAPI="https://pokeapi.co/api/v2"
:::caution
以这种方式设置的变量将在你的项目中随处可用,包括在客户端上。
:::

## 获取环境变量

在 Vite 中,你可以使用 `import.meta.env`,而不是使用 `process.env`,它使用了 ES2020 中增加的 `import.meta` 特性。
Expand All @@ -91,6 +92,9 @@ const data = fetch(`${import.meta.env.PUBLIC_POKEAPI}/pokemon/squirtle`);
因为 Vite 静态地替换了 `import.meta.env`,所以你不能用 `import.meta.env[key]` 这样的动态键来访问它。
:::
使用 SSR 时,可以根据所使用的 SSR 适配器,在运行时访问环境变量。对于大部分适配器,可以通过 `process.env` 访问环境变量,而 Deno 适配器则需要使用 `Deno.env.get()`。Cloudflare 有它[自己的方式](/zh-cn/guides/integrations-guide/cloudflare/#环境变量) 来处理环境变量。
Astro 会检查服务器环境中的变量,如果这些变量不存在,则会在 `.env` 文件中查找它们。
## TypeScript 智能提示
默认情况,Astro 在 `astro/client.d.ts` 中为 `import.meta.env` 提供类型定义。
Expand Down

0 comments on commit 50b41a1

Please sign in to comment.