如何从中间件或插件设置 nuxtjs 标题和元?



如何从nuxt-middleware/nuxt-plugin设置NuxtJS标题%元标记?

目前我正在从nuxt.config进行设置.js如下所示。

head: () => {
const now = Date.now()
return {
title: `config test : ${now}`,
meta: [
{
hid: 'description',
name: 'description',
content: 'My custom description'
}
]
}
},

请注意,我知道我可以在页面组件中进行类似的设置。我不想在每个页面中设置标题功能,而是想要一个公共位置,在那里我可以使用路由器实例动态设置标题。这是在路由器中间件或插件中。

您可以根据您所在的路线在中间件中计算您的头部信息。然后你可以将这些数据存储在你的 vuex 中,或者把它作为道具传递给页面。在您的页面中,您可以在 asyncData 挂钩中获取此数据。头部碰撞的一个例子是:

export default function ({ store, route }) {
const headInfoCollection = [
{ name: 'home', title: "Home" },
{ name: 'about', title: "About" }
]
const headInfo = headInfo.find(c => c.name === route.name)

store.dispatch('setHeadInfo', headInfo)
}

最新更新