What is the suggested alternative to layout middlewares in Nuxt 3?
In Nuxt 2 it was possible to define middlewares on layouts, this is no longer supported with Nuxt 3.
I would like to avoid .global middleware, as these would be called on all pages, even if not needed?
5 Replies
wait are you sure this is not supported in layouts?
look 1 second google https://stackoverflow.com/questions/73293352/nuxt-3-how-to-use-route-middleware-in-a-layout-can-i
Stack Overflow
NUXT 3: How to use route middleware in a layout? (Can I?)
I've been looking to use Nuxt middleware in a layout. But I am not sure if I even can, however, since I used it in Nuxt 2, it may be possible in Nuxt 3.
The project has 2 different layouts: Public....
Yes, this is definetely not possible. You can see the error page is telling, that it only works for SFC that are also a page. I found that stackoverflow search, but i did not want to use
.global
as stated in my original question. I don't want to call the middleware on every page. And I also don't want to nest my pages in /admin
, thats why I asked here as well.
I ended up using .global
middleware, since I didn't found any alternative. Thanks for your help tho!Can you define it in something like this?
it seems like the error is coming from importing the definePageMeta instead of defining the middleware it self
@wypratama Sorry for my late response. And even though it throws no error, it still doesn't work. Nothing happens.
Also I am using
Also I am using
<script lang="ts" setup>
and there you cannot return export default
. But I also tried it without export default
and also using it without script setup notation. No success. I also tried defineComponent
. It doesn't seem to be possible.