首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用vue-i18n更改属性lang html

如何使用vue-i18n更改属性lang html
EN

Stack Overflow用户
提问于 2019-09-06 12:14:40
回答 2查看 2.1K关注 0票数 1

我想将html元素的属性从:

代码语言:javascript
复制
lang='en'

代码语言:javascript
复制
lang='jp'

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

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

代码语言:javascript
复制
 htmlAttrs: {
    lang: this.$i18n.locale,
 },

head对象中但引发错误:无法读取未定义的属性“”$i18n“”

这是我的vue-i18n插件:

代码语言:javascript
复制
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'),
        },
    });
};

感谢您的阅读!

EN

回答 2

Stack Overflow用户

发布于 2020-04-23 01:03:37

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

代码语言:javascript
复制
i18n: {
   seo: true,
   ...
}

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

票数 1
EN

Stack Overflow用户

发布于 2020-04-04 17:32:11

如果您考虑不在nuxt.config.js中使用htmlAttrs,而是在其他文件中使用htmlAttrs (例如,我在我的唯一布局中使用MainHeader.vue中的htmlAttrs),则代码如下:

代码语言:javascript
复制
export default {
  name: 'MainHeader',
  head() {
    return {
      meta: [{
        hid: 'og:locale', property: 'og:locale', content: this.currentLocaleDecoding
      }],
      htmlAttrs: {
        lang: this.currentLocale ? this.currentLocale : 'en'
      }
    }
  },
...
  computed: {
    currentLocale() {
      return this.$i18n.locale
    },
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57815643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档