创建生产版本时出现错误“不包含默认导出”

时间:2020-03-19 04:53:04

标签: reactjs react-intl react-scripts

Hii我正在使用"react-intl": "^4.1.1",进行语言翻译,但是当我创建生产版本时,代码不会编译并显示关于默认导出的错误

错误:

Creating an optimized production build...
Failed to compile.

./src/lang/entries/es_ES.js
Attempted import error: '../locales/es_ES' does not contain a default export (imported as 'enMessages').

我正在这样导入文件:

import enMessages from '../locales/en_US';

导出:

export default {
   "sidebar.dashboard" : "Dashboard"
}

最重要的是它在创建生产版本时处理本地问题并生成问题,我在此项目中使用"react-scripts": "^3.3.0"。我该如何解决?

更新: 导入enMessage后

import endata from '@formatjs/intl-pluralrules/dist/locale-data/en';
import enMessages from '../locales/en_US';

const EnLang = {
   messages: {
      ...enMessages
   },
   locale: 'en-US',
   data: endata
};
export default EnLang;

2 个答案:

答案 0 :(得分:0)

您的代码看起来不错。您是否尝试过命名出口?

/** ./src/lang/entries/es_ES.js */
import { enMessages } from '../locales/en_US

/** ./src/lang/locales/en_US.js */
export const enMessages = { "sidebar.dashboard" : "Dashboard" } 

默认导出为generally a bad idea ...且模棱两可。是什么阻止某人这样进口?

import esMessages from '../locales/en_US

答案 1 :(得分:0)

尝试一下...

// ./src/lang/locales/en_US.js
export default {
   // We are trying to export a `key`, initiated as a String
-  "sidebar.dashboard" : "Dashboard",

   // let's treat sidebar/dashboard as plain keys
+  sidebar: {
+     dashboard: "Dashboard"
+  }
}

并考虑如下使用...

// ./src/lang/entries/es_ES.js
import enMessages from '../locales/en_US';

// Based on how you are using `enMessages`
+ export default { // best to use this to avoid `conflict` in later use
+  ...enMessages,  // destructure directly here
   locale: 'en-US',
   data: endata
};

// Use this for reference (though )
<Component id="sidebar.dashboard" />