nuxt.js - 如何使用 vue-i18n 更改属性 lang html

标签 nuxt.js

我想更改 html 元素的属性:

lang='en'

lang='jp'

当我使用 vue-i18n 包更改语言时。

我在 nuxt.config.js 中导入插件 vue-i18n 并添加属性:

 htmlAttrs: {
    lang: this.$i18n.locale,
 },

head 对象中但它抛出错误:无法读取未定义的属性 '$i18n'

这是我的 vue-i18n 插件:

import Vue from 'vue';
import VueI18n from 'vue-i18n';

Vue.use(VueI18n);

export default ({ app, store }) => {
    app.i18n = new VueI18n({
        locale: store.state.i18n.locale,
        fallbackLocale: 'vi',
        messages: {
            en: require('~/locales/en.json'),
            vi: require('~/locales/vi.json'),
            jp: require('~/locales/jp.json'),
        },
    });
};

感谢您的阅读!

最佳答案

nuxt.config.js 文件的 i18n 配置部分,将 seo 属性设置为 true 并且 i18n 模块应该采用为您设置 html lang 属性。

i18n: {
   seo: true,
   ...
}

Nuxt i18n 文档:https://nuxt-community.github.io/nuxt-i18n/seo.html#benefits

关于nuxt.js - 如何使用 vue-i18n 更改属性 lang html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57815643/

相关文章:

css - NuxtJS css 提取如何用于生成的静态网站?

vue.js - 如何在 Nuxt.js 中创建框架加载?

vue.js - 如何处理来自 Nuxt SSR 的 Passport js 重定向?

javascript - 窗口未定义,Vue 平滑滚动 - Nuxt.js

javascript - Nuxt.js:如何跟踪 $route.params.someKey 的值?

javascript - Nuxt Js asyncData 返回值表现得很奇怪。

nuxt.js - Nuxt 3 为整个应用程序全局导入 lodash

javascript - Vue 和 Nuxt Js : why Cannot create property 'display' on string 'bottom:30px;right:30px;' ?

vue.js - Nuxt中路由更改后如何不向下滚动页面?

service-worker - 在 nuxtjs 应用程序中禁用服务人员或工作箱