在 create-react-app 中使用来自“./Name 的导出名称?

IT技术 reactjs create-react-app
2021-05-06 06:48:46

我正在尝试将 React 应用程序移动到 Create-React-App 中,但正在努力使某些事情发挥作用,并且并不真正了解 Create-React-App 允许和不允许的内容(关于自定义和添加额外module)。

目前,来自 React 应用程序的 CRA 无法在以下位置编译:

export App from "./App/App"

有错误:

./src/containers/index.js
SyntaxError: /Volumes/Mexico/Workspace/create-react-app-test/myapp/src/containers/index.js: Support for the experimental syntax 'exportDefaultFrom' isn't currently enabled (1:8):

Add @babel/plugin-proposal-export-default-from (https://git.io/vb4yH) to the 'plugins' section of your Babel config to enable transformation.

我不想从 CRA 退出,我不相信我可以更改 Babel 配置,所以我不知道该怎么做。

CRA 支持这种语法吗?如果是这样,我应该怎么做才能让它工作。如果没有,我是否必须重写所有代码?

react脚本 2.1.8 节点 v11.14.0 npm 6.9.0

谢谢,阿什利。

3个回答

export App from "./App/App"不在受支持的export语法变体中。

为了符合规范,它可以更改为:

export { default as App } from "./App/App"

这些是CRA 支持的功能列表不支持您尝试使用的语法。要么弹出并更改,要么想办法修改 babel 配置。

你可以做类似的事情

export {default as App} from './App/App';
export {default as About} from './About/About';
.
.
.