useRuntimeConfig

使用 useRuntimeConfig 可组合函数访问运行时配置变量。

用法

¥Usage

app.vue
<script setup lang="ts">
const config = useRuntimeConfig()
</script>
server/api/foo.ts
export default defineEventHandler((event) => {
  const config = useRuntimeConfig(event)
})
Read more in Docs > Guide > Going Further > Runtime Config.

定义运行时配置

¥Define Runtime Config

以下示例展示了如何设置公共 API 基 URL 和只能在服务器上访问的秘密 API 令牌。

¥The example below shows how to set a public API base URL and a secret API token that is only accessible on the server.

我们应该始终在 nuxt.config 中定义 runtimeConfig 变量。

¥We should always define runtimeConfig variables inside nuxt.config.

nuxt.config.ts
export default defineNuxtConfig({
  runtimeConfig: {
    // Private keys are only available on the server
    apiSecret: '123',

    // Public keys that are exposed to the client
    public: {
      apiBase: process.env.NUXT_PUBLIC_API_BASE || '/api'
    }
  }
})
需要在服务器上可访问的变量直接添加到 runtimeConfig 中。需要在客户端和服务器上均可访问的变量在 runtimeConfig.public 中定义。
Read more in Docs > Guide > Going Further > Runtime Config.

访问运行时配置

¥Access Runtime Config

要访问运行时配置,我们可以使用 useRuntimeConfig() 可组合函数:

¥To access runtime config, we can use useRuntimeConfig() composable:

server/api/test.ts
export default defineEventHandler((event) => {
  const config = useRuntimeConfig(event)

  // Access public variables
  const result = await $fetch(`/test`, {
    baseURL: config.public.apiBase,
    headers: {
      // Access a private variable (only available on the server)
      Authorization: `Bearer ${config.apiSecret}`
    }
  })
  return result
}

在此示例中,由于 apiBase 是在 public 命名空间内定义的,因此它在服务器端和客户端均可访问,而 apiSecret 只能在服务器端访问。

¥In this example, since apiBase is defined within the public namespace, it is universally accessible on both server and client-side, while apiSecret is only accessible on the server-side.

环境变量

¥Environment Variables

可以使用以 NUXT_ 为前缀的匹配环境变量名来更新运行时配置值。

¥It is possible to update runtime config values using a matching environment variable name prefixed with NUXT_.

Read more in Docs > Guide > Going Further > Runtime Config.

使用 .env 文件

¥Using the .env File

我们可以在 .env 文件中设置环境变量,以便在开发和构建/生成过程中访问它们。

¥We can set the environment variables inside the .env file to make them accessible during development and build/generate.

.env
NUXT_PUBLIC_API_BASE = "https://api.localhost:5555"
NUXT_API_SECRET = "123"
developmentbuild/generate 期间,在 Nuxt 应用中,可以使用 process.env 访问 .env 文件中设置的任何环境变量。
在生产运行时,你应该使用平台环境变量,并且不使用 .env。¥In production runtime, you should use platform environment variables and .env is not used.
Read more in Docs > Guide > Directory Structure > Env.

app 命名空间

¥app namespace

Nuxt 在运行时配置中使用 app 命名空间,其键包含 baseURLcdnURL。你可以通过设置环境变量在运行时自定义它们的值。

¥Nuxt uses app namespace in runtime-config with keys including baseURL and cdnURL. You can customize their values at runtime by setting environment variables.

这是一个保留命名空间。你不应在 app 中引入其他键。

app.baseURL

默认情况下,baseURL 设置为 '/'

¥By default, the baseURL is set to '/'.

但是,可以通过将 NUXT_APP_BASE_URL 设置为环境变量来在运行时更新 baseURL

¥However, the baseURL can be updated at runtime by setting the NUXT_APP_BASE_URL as an environment variable.

然后,你可以使用 config.app.baseURL 访问这个新的基本 URL:

¥Then, you can access this new base URL using config.app.baseURL:

/plugins/my-plugin.ts
export default defineNuxtPlugin((NuxtApp) => {
  const config = useRuntimeConfig()

  // Access baseURL universally
  const baseURL = config.app.baseURL
})

app.cdnURL

此示例展示了如何设置自定义 CDN URL 并使用 useRuntimeConfig() 访问它们。

¥This example shows how to set a custom CDN url and access them using useRuntimeConfig().

你可以使用自定义 CDN,通过 NUXT_APP_CDN_URL 环境变量在 .output/public 内部提供静态资源。

¥You can use a custom CDN for serving static assets inside .output/public using the NUXT_APP_CDN_URL environment variable.

然后使用 config.app.cdnURL 访问新的 CDN 网址。

¥And then access the new CDN url using config.app.cdnURL.

server/api/foo.ts
export default defineEventHandler((event) => {
  const config = useRuntimeConfig(event)

  // Access cdnURL universally
  const cdnURL = config.app.cdnURL
})
Read more in Docs > Guide > Going Further > Runtime Config.