6

我正在使用 nuxtjs 2.10.x 和 i18n 模块。Nu 自定义中间件或类似的东西。路由工作正常。

我的nuxt.config.js模块/i18n 部分:

    ...
modules: [
'@nuxtjs/axios',
'@nuxtjs/pwa',
'@nuxtjs/auth',
'@nuxtjs/dotenv',
'nuxt-fontawesome',
[
  'nuxt-i18n',
  {
    locales: [
      {
        code: 'en',
        iso: 'en-US',
        file: 'en.json',
        name: 'English'
      },
      {
        code: 'zh',
        iso: 'zh-CN',
        file: 'zh.json',
        name: '简体中文'
      }
    ],
    lazy: true,
    langDir: 'locales/',
    defaultLocale: 'en',
    strategy: 'prefix_except_default',
    differentDomains: false,
    vueI18n: {
      fallbackLocale: 'en'
    },
    detectBrowserLanguage: {
      useCookie: true,
      cookieKey: 'lang'
    }
  }
    ]
  ],
...

页面文件夹结构:

  'pages/'
    |--'contact_us.vue'
    |--'_lang/'
         |--'contact_us.vue'

但我收到了这个疯狂的警告:[vue-router] Route with name 'contact_us___en' does not exist. 实际上 nuxt 对我拥有的所有页面都给出了类似的警告。而且没有任何线索为什么会这样。什么可能是错的?

4

2 回答 2

12

此错误是由localePath()用于为当前语言环境生成 url 的函数引发的。

奇怪的是,它不能通过操纵传入的 url 来工作,而是通过尝试匹配在 vue 路由器中定义的路由的 name 属性:

在此处输入图像描述

这意味着要找到页面的 url,我们需要将其编写如下:

localePath('index')       >>>   "/"
localePath('login')       >>>   "/login"
localePath('tech-test')   >>>   "/tech/test"

以下方式会抛出错误并默认为'/'

localePath('/')            >>>   "/"
localePath('/login')       >>>   "/"
localePath('tech/test')    >>>   "/"

文档:https ://nuxt-community.github.io/nuxt-i18n/basic-usage.html#nuxt-link

编辑: 我已经提交了一个允许使用路径作为字符串的请求:

localePath('/')          >>>   "/"
localePath('/tech/test') >>>   "/tech/test"

https://github.com/nuxt-community/nuxt-i18n/pull/554

于 2019-12-27T08:21:17.543 回答
0
 <nuxt-link :to="localePath({ name: 'your-dynamic-path' })">
    Some text...
  </nuxt-link>

这就是我配置我的方式。

于 2021-01-30T12:33:02.373 回答