mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 23:52:06 +00:00
f26a801775
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Roe <daniel@roe.dev>
48 lines
1.7 KiB
Markdown
48 lines
1.7 KiB
Markdown
---
|
|
title: Plugins and Middleware
|
|
description: 'Learn how to migrate from Nuxt 2 to Nuxt Bridge new plugins and middleware.'
|
|
---
|
|
|
|
## New Plugins Format
|
|
|
|
You can now migrate to the Nuxt 3 plugins API, which is slightly different in format from Nuxt 2.
|
|
|
|
Plugins now take only one argument (`nuxtApp`). You can find out more in [the docs](/docs/guide/directory-structure/plugins).
|
|
|
|
```js [plugins/hello.ts]
|
|
export default defineNuxtPlugin(nuxtApp => {
|
|
nuxtApp.provide('injected', () => 'my injected function')
|
|
// now available on `nuxtApp.$injected`
|
|
})
|
|
```
|
|
|
|
::callout
|
|
If you want to use the new Nuxt composables (such as [`useNuxtApp`](/docs/api/composables/use-nuxt-app) or `useRuntimeConfig`) within your plugins, you will need to use the `defineNuxtPlugin` helper for those plugins.
|
|
::
|
|
|
|
::callout{color="amber" icon="i-ph-warning-duotone"}
|
|
Although a compatibility interface is provided via `nuxtApp.vueApp` you should avoid registering plugins, directives, mixins or components this way without adding your own logic to ensure they are not installed more than once, or this may cause a memory leak.
|
|
::
|
|
|
|
## New Middleware Format
|
|
|
|
You can now migrate to the Nuxt 3 middleware API, which is slightly different in format from Nuxt 2.
|
|
|
|
Middleware now take only two argument (`to`, `from`). You can find out more in [the docs](/docs/guide/directory-structure/middleware).
|
|
|
|
```js
|
|
export default defineNuxtRouteMiddleware((to) => {
|
|
if (to.path !== '/') {
|
|
return navigateTo('/')
|
|
}
|
|
})
|
|
```
|
|
|
|
::callout{color="amber" icon="i-ph-warning-duotone"}
|
|
Use of `defineNuxtRouteMiddleware` is not supported outside of the middleware directory.
|
|
::
|
|
|
|
::callout{color="amber" icon="i-ph-warning-duotone"}
|
|
Nuxt Bridge does not support `definePageMeta`.
|
|
::
|