英文:
How to set the page 'to.meta' attribute in the RouteMiddleware
问题
I have the following routeMiddleware:
export default defineNuxtRouteMiddleware(async (to, from) => {
console.log(to);
useHead({
title: `MYApp - ${(String(to.meta.name))}`,
})
}
How can I change the title page-per-page? For example: I have an index page and a product page. I would like the to.name
on my index page to be called 'MYApp - Home'.
And the to.name
on my product page to be called 'MYApp - Shop'.
I have tried adding the following to my index page, but to.meta
remains empty:
useHead({
meta: [
{ name: 'Home', content: 'Content' }
],
})
How can I do this?
SOLUTION: found it. It's not useHead()
, it's definePageMeta()
.
英文:
I have the following routeMiddleware:
export default defineNuxtRouteMiddleware(async (to, from) => {
console.log(to);
useHead({
title: `MYApp - ${(String(to.meta.name))}`,
})
}
How can I change the title page-per-page? For example: I have an index page and a product page. I would like the to.name on my index page to be called 'MYApp - Home'.
And the to.name on my product page to be called 'MYApp - Shop'.
I have tried adding the following to my index page, but to.meta remains empty:
useHead({
meta: [
{ name: 'Home', content: 'Content' }
],
})
How can I do this?
SOLUTION: found it. It's not useHead(), it's definePageMeta().
答案1
得分: 1
使用 useHead()
代替,我需要使用 definePageMeta()
。
有了这段代码,我可以在我的中间件中访问它。
definePageMeta({
title: 'Home',
});
英文:
Instead of using useHead()
, I need to use definePageMeta()
.
With this code I can access it in my middleware.
definePageMeta({
title: 'Home',
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论