我们有一个模块,看起来像
export default class DemoComponent extend React.Component { render() { return( <h1>Hello</h1> ); } } export default connect( mapStateToProps,{ ... } )(DemoComponent);
在我们进行npm安装之前,它曾经工作正常,它加载了新版本的开发依赖项.
错误日志:
Only one default export allowed per module.
at File.buildCodeFrameError (/home/workspace/node_modules/babel-core/lib/transformation/file/index.js:431:15)
at NodePath.buildCodeFrameError (/home/workspace/node_modules/babel-traverse/lib/path/index.js:140:26)
at PluginPass.exit (/home/workspace/node_modules/babel-plugin-transform-es2015-modules-commonjs/lib/index.js:253:29)
at newFn (/home/workspace/node_modules/babel-traverse/lib/visitors.js:276:21)
at NodePath._call (/home/workspace/node_modules/babel-traverse/lib/path/context.js:76:18)
at NodePath.call (/home/workspace/node_modules/babel-traverse/lib/path/context.js:48:17)
at NodePath.visit (/home/workspace/node_modules/babel-traverse/lib/path/context.js:117:8)
at TraversalContext.visitQueue (/home/workspace/node_modules/babel-traverse/lib/context.js:150:16)
at TraversalContext.visitSingle (/home/workspace/node_modules/babel-traverse/lib/context.js:108:19)
at TraversalContext.visit (/home/workspace/node_modules/babel-traverse/lib/context.js:192:19)
at Function.traverse.node (/home/workspace/node_modules/babel-traverse/lib/index.js:161:17)
解决方法
DemoComponent.js
export class DemoComponent extends React.Component { render() { return( <h1>Hello</h1> ); } } export default connect( mapStateToProps,{ ... } )(DemoComponent);
所以import语句如下所示:
import ConnectedComponent,{DemoComponent} from './DemoComponent';
当您使用导出默认值时,您可以根据需要命名变量,但使用命名导出,您必须使用与导出的变量名相同的变量名称.
Btw你在你的例子中有错字.它是扩展,不扩展