我需要在 Nuxt 中为我的应用程序设置全局头,一些子页面会覆盖它。那些全局头需要包含翻译的数据。
我seoHead.js
用代码创建了文件:
import Vue from "vue";
export const $t = (sign) => Vue.prototype.$nuxt.$options.i18n.t(sign);
export default {
title: $t("seoGlobal.title"),
meta: [
{ charset: "utf-8" },
{ name: "viewport", content: "width=device-width, initial-scale=1" },
{
hid: "description",
name: "description",
content: $t("seoGlobal.description"),
},
{
hid: "ogSiteName",
name: "og:site_name",
content: "Test Page",
},
{
hid: "ogTitle",
name: "og:title",
content: $t("seoGlobal.ogTitle"),
},
(...)
],
};
我在我的index.vue
页面和其他页面中导入并使用这些数据,如下所示:
import seoHead from "~/constants/seoHead";
export default {
head() {
const metaI18n = this.$nuxtI18nSeo();
const currentPath = process.env.LP_URL + this.$router.currentRoute.fullPath;
return {
...seoHead,
meta: [
{
hid: "ogLocale",
name: "og:locale",
content: metaI18n.meta[0].content,
},
{
hid: "ogLocaleAlternate",
name: "og:locale:alternate",
content: metaI18n.meta[1].content,
},
{
hid: "ogUrl",
name: "og:url",
content: currentPath,
},
],
};
},
(...)
不幸的是,我面临Cannot read property '$options' of undefined
错误。这对我来说很奇怪,因为我已经export const $t = (sign) => Vue.prototype.$nuxt.$options.i18n.t(sign);
在另一个 js 文件中使用了代码。有谁知道为什么会出现这个错误?您知道翻译全局头部选项的最佳方式吗?