如何在RouteMiddleware中设置页面的 ‘to.meta’ 属性。

huangapple go评论64阅读模式
英文:

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',
});

huangapple
  • 本文由 发表于 2023年2月8日 17:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定