在哪里执行方法来保留nuxt-i18n中的语言环境更改?



我正在遵循有关如何更改语言(https://nuxt-community.github.io/nuxt-i18n/lang-switcher.html(的文档,它工作得很好。但是每当我将nuxt.config.js中的detectBrowserLanguage.useCookiedetectBrowserLanguage.alwaysRedirect设置为 true 时,我都应该调用this.$i18n.setLocaleCookie(locale)方法来保留更改(如文档所示(。

我唯一的问题是,我应该在哪里调用此方法?

我当前的代码:

<nuxt-link
class="px-6 py-2 block"
:click="this.$i18n.setLocaleCookie(locale)"
v-for="locale in availableLocales"
:key="locale.code"
:to="switchLocalePath(locale.code)">{{ locale.code.toUpperCase() }}

export default {
computed: {
availableLocales () {
return this.$i18n.locales.filter(i => i.code !== this.$i18n.locale)
}
}
}

如您所见,我尝试在链接的点击事件上运行this.$i18n.setLocaleCookie(locale),但我得到了Cannot read property '$i18n' of undefined

尝试在插件中调用它

export default async function ({app, store}) {
// on change locale
app.i18n.beforeLanguageSwitch = (oldLocale, newLocale) => {
...
}
}

nuxt.config.js

plugins: [
'~/plugins/i18n.js'
],

最新更新