|
1 | 1 | # `definePageMeta`
|
2 | 2 |
|
3 |
| -::ReadMore{link="/guide/features/routing"} |
| 3 | +`definePageMeta` is a compiler macro that you can use to set metadata for each **page** component located in the `pages/` directory. This way you can set custom metadata for each static or dynamic route of your Nuxt application. |
| 4 | + |
| 5 | +::ReadMore{link="/guide/directory-structure/pages"} |
4 | 6 | ::
|
5 | 7 |
|
6 |
| -::NeedContribution |
| 8 | +## Usage |
| 9 | + |
| 10 | +```vue [pages/some-page.vue] |
| 11 | +<script setup> |
| 12 | + definePageMeta({ |
| 13 | + title: 'Articles' |
| 14 | + }) |
| 15 | +</script> |
| 16 | +``` |
| 17 | + |
| 18 | +> `definePageMeta` works in both `<script setup>` and `<script>`. |
| 19 | +
|
| 20 | +## Properties |
| 21 | + |
| 22 | +You can set following properties through `definePageMeta` . |
| 23 | + |
| 24 | +- **title** - `string` |
| 25 | +Set custom title for each route of your application |
| 26 | + |
| 27 | +- **key** - `string | function` |
| 28 | + |
| 29 | +Set `key` value when you need more control over when the `<NuxtPage>` component is re-rendered. |
| 30 | + |
| 31 | +- **layout** - `boolean | string` |
| 32 | + |
| 33 | +Set a static or dynamic name of the layout for each route. This can be set to `false` as well in case the default layout needs to be disabled. |
| 34 | + |
| 35 | +- **middleware**: `string | function` |
| 36 | + |
| 37 | +Define anonymous or named middleware directly within `definePageMeta` under `middleware` key. Learn more about route middleware. |
| 38 | + |
| 39 | +- **layoutTransition**: `boolean | TransitionProps` |
| 40 | + |
| 41 | +Set name of the transition to apply for current layout. You can also set this value to `false` to disable the layout transition. |
| 42 | +(Learn more about [TransitionProps](https://github.com/vuejs/vue/blob/main/src/platforms/web/runtime/components/transition.ts)). |
| 43 | + |
| 44 | +- **pageTransition**: `boolean | TransitionProps` |
| 45 | + |
| 46 | +Set name of the transition to apply for current page. You can also set this value to `false` to disable the page transition. |
| 47 | + |
| 48 | +- **alias**: `string | string[]` |
| 49 | +Set alias to access the same page from different paths. Learn more about alias [alias](https://router.vuejs.org/guide/essentials/redirect-and-alias.html#alias) |
| 50 | + |
| 51 | +- **keepalive**: `boolean | KeepAliveProps` |
| 52 | + |
| 53 | +Set to `true` when you want to preserve page state across route changes. |
| 54 | +(Learn more about [KeepAlive](https://vuejs.org/api/built-in-components.html#keepalive)) |
| 55 | + |
| 56 | +Apart from above values, you can set **custom** values in `definePageMeta` as well depending on your use-case. This data can then be accessed throughout your Nuxt application using `route.meta` object as shown below. |
| 57 | + |
| 58 | +```vue [pages/some-page.vue] |
| 59 | +<script setup> |
| 60 | + const route = useRoute() |
| 61 | + console.log(route.meta.title) |
| 62 | +</script> |
| 63 | +``` |
| 64 | + |
| 65 | +## Examples |
| 66 | + |
| 67 | +### Basic usage |
| 68 | + |
| 69 | +The example below shows how `key` can be a function that returns a value, while `pageType` is a custom property and `keepAlive` property makes sure that the `modal` component is not cached when you change pages. |
| 70 | + |
| 71 | +```vue [pages/some-page.vue] |
| 72 | +<script setup> |
| 73 | + definePageMeta({ |
| 74 | + title: 'Checkout home page', |
| 75 | + key: route => route.fullPath, |
| 76 | +
|
| 77 | + keepalive: { |
| 78 | + exclude: ['modal'] |
| 79 | + }, |
| 80 | +
|
| 81 | + // custom property: pageType |
| 82 | + pageType: "Checkout" |
| 83 | + }) |
| 84 | +</script> |
| 85 | +``` |
| 86 | + |
| 87 | +### Define m**iddleware** |
| 88 | + |
| 89 | +The example below shows how the middleware can be defined using a `function` directly within the `definePageMeta` or set as a `string` that matches the middleware file name located in the `middleware/` directory. |
| 90 | + |
| 91 | +```vue [pages/some-page.vue] |
| 92 | +<script setup> |
| 93 | + definePageMeta({ |
| 94 | +
|
| 95 | + // function |
| 96 | + middleware: [ |
| 97 | + async function (to, from) { |
| 98 | + const auth = useState('auth') |
| 99 | + if (!auth.value.authenticated) { |
| 100 | + return navigateTo('/login') |
| 101 | + } |
| 102 | + return navigateTo('/checkout') |
| 103 | + } |
| 104 | + ], |
| 105 | +
|
| 106 | + // string |
| 107 | + // middleware: ["auth"] |
| 108 | + // or middleware: "auth" |
| 109 | +}) |
| 110 | +</script> |
| 111 | +``` |
| 112 | + |
| 113 | +### Define layout |
| 114 | + |
| 115 | +You can set the `layout` as a `string` value that matches the layout file located in `layouts/` directory. You can disable the layout by setting the `layout` to `false`. |
| 116 | + |
| 117 | +```vue [pages/some-page.vue] |
| 118 | +<script setup> |
| 119 | + function applyCustomLayout () { |
| 120 | + route.meta.layout = "custom" |
| 121 | + } |
| 122 | +
|
| 123 | + definePageMeta({ |
| 124 | + layout: false |
| 125 | + // or layout: "custom" |
| 126 | + }) |
| 127 | +</script> |
| 128 | +``` |
| 129 | + |
| 130 | +::ReadMore{link="/guide/features/routing"} |
7 | 131 | ::
|
0 commit comments