NuxtJS i18n [vue-router] 名称为 about_us___en 的路由不存在

Shi*_*ker 6 vue.js nuxt.js nuxt-i18n

我使用 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'
    }
  }
    ]
  ],
...
Run Code Online (Sandbox Code Playgroud)

页面文件夹结构:

  'pages/'
    |--'contact_us.vue'
    |--'_lang/'
         |--'contact_us.vue'
Run Code Online (Sandbox Code Playgroud)

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

Tim*_*tis 12

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

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

在此处输入图片说明

这意味着要找到页面的 url,我们需要按如下方式编写它:

localePath('index')       >>>   "/"
localePath('login')       >>>   "/login"
localePath('tech-test')   >>>   "/tech/test"
Run Code Online (Sandbox Code Playgroud)

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

localePath('/')            >>>   "/"
localePath('/login')       >>>   "/"
localePath('tech/test')    >>>   "/"
Run Code Online (Sandbox Code Playgroud)

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

编辑: 我提交了一个 pullrequest,允许使用路径作为字符串:

localePath('/')          >>>   "/"
localePath('/tech/test') >>>   "/tech/test"
Run Code Online (Sandbox Code Playgroud)

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