无法访问nuxt-i18n信息

时间:2020-08-29 07:31:33

标签: nuxt.js nuxt-i18n

我正在nuxt中创建一个国际博客。我似乎无法访问导航组件和某些页面上的i18n对象。您能否阐明如何访问它以及为什么我不能访问它?我假设它与生命周期有关,但不确定...

在nuxt-config.js中:

...
  modules: [
    'nuxt-i18n',
    '@nuxt/content'
  ],
  i18n:{
    strategy: 'prefix_and_default',
    defaultLocale: 'en',
    langDir: 'lang/',
    lazy: true,
    locales: [
      {
        name: '日本語',
        code: 'ja',
        iso: 'ja-JP',
        file: 'ja-JP.js'
      },
      {
        name: 'English',
        code: 'en',
        iso: 'en-US',
        file: 'en-US.js'
      },
    ],
    detectBrowserLanguage: {
      // If enabled, a cookie is set once a user has been redirected to his
      // preferred language to prevent subsequent redirections
      // Set to false to redirect every time
      useCookie: true,
      // Set to override the default domain of the cookie. Defaults to host of the site.
      cookieDomain: null,
      // Cookie name
      cookieKey: 'i18n_redirected',
      // Set to always redirect to value stored in the cookie, not just once
      alwaysRedirect: false,
      // If no locale for the browsers locale is a match, use this one as a fallback
    }
  },
...

我试图根据语言引入不同的降价文件,所以我打电话:

export default {
    async asyncData({ $content, params}) {
      const data = await $content(`${i18n.locale}/data`, params.slug).fetch()
      return { data }
    },

在内容上我有en/data/...ja/data/...,如果我将其硬编码为en/data/ja/data,它可以工作,但是我得到的是i18n是未定义的错误,我尝试了几次模式,但没有成功。

1 个答案:

答案 0 :(得分:0)

我认为最好在您的代码中使用这种结构

https://nuxtjs.org/examples/i18n/