javascript - 错误: Module should export a function: i18next

标签 javascript plugins i18next nuxt.js

在我的 Nuxt js 应用程序中,我安装了 i18next :

npm install --save-dev i18next

然后每当我将其添加到 nuxt.config.js 中的插件数组时,作为 documentation建议:

module.exports = {
  build: {
    vendor: ['i18next']
  }
}

启动服务器时出现此错误(npm run dev)

 ERROR  Nuxt error

  Error: Module should export a function: i18next

  - module.js:127 ModuleContainer.addModule
    [begueradj]/[nuxt]/lib/core/module.js:127:13

  - utils.js:96 promise.then
    [begueradj]/[nuxt]/lib/common/utils.js:96:43


  - next_tick.js:189 process._tickCallback
    internal/process/next_tick.js:189:7

  - module.js:696 Function.Module.runMain
    module.js:696:11

  - bootstrap_node.js:204 startup
    bootstrap_node.js:204:16

  - bootstrap_node.js:625 
    bootstrap_node.js:625:3

为什么会发生这种情况?如何解决?

最佳答案

Nuxt.js 1.x 上使用 vendor 数组来帮助 Webpack 3 优化构建。它不用于导入库。

(注意:自 Nuxt.js 2.x 起,该 vendor 配置已弃用,可以删除)

<小时/>

要导入外部库,您需要创建一个自定义 Vue.js 插件,并在 plugins 数组的 nuxt.config.js 中声明它 ( https://nuxtjs.org/guide/plugins/ )

module.exports = {
  plugins: ['~/plugins/your-cutom-plugins']
}

或者,

您可以在组件/页面/中间件/插件文件中导入外部库以直接使用它:

<script>
import i18next from 'i18next'
​
i18next.init({ 
   ...
)
</script>

(注意:更喜欢使用 install --save 因为“i18next”不仅用于开发,还用于生产)

关于javascript - 错误: Module should export a function: i18next,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52532101/

相关文章:

internet-explorer - 您如何为IE开发插件?

javascript - AngularJS 继承模式不使用作用域?

javascript - 获得一个工作 meteor JS Vagrant 盒子

objective-c - 如何编写 OS X Finder 插件

ASP.NET MVC下的JQuery菜单插件似乎只能在Chrome中工作,但不能在IE和FireFox中工作

javascript - I18N 在 Next.JS 中更改语言

javascript - i18next - 在 data-i18n-options 属性中提供用于插值的键

javascript - 使用 Express 中的 i18n-2 创建多语言 Web 应用程序

javascript - 了解 "find longest word in string"代码

javascript - Sencha Touch - 从数字字段获取值