docs: add example of useRuntimeConfig() within a plugin (#2900)

This commit is contained in:
notRyan 2022-01-25 13:04:22 +01:00 committed by GitHub
parent 29fa0c991b
commit a569d16900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -77,6 +77,21 @@ const config = useRuntimeConfig()
**`useRuntimeConfig` only works during `setup` or `Lifecycle Hooks`**.
::
### Plugins
If you want to use the runtime confg within any (custom) plugin, you can use `useRuntimeConfig()` inside of your `defineNuxtPlugin` function.
For example:
```ts
export default defineNuxtPlugin((nuxtApp) => {
const config = useRuntimeConfig();
const url = process.server ? config.serverUrl : config.clientUrl;
// Do something with url & isServer.
});
```
### API routes
Within the API routes, you can access runtime config by directly importing from virtual `#config`.