![](/img/trans.png)
[英]Attempted import error: './components' does not contain a default export (imported as 'App')
[英]React-Redux: Attempted import error: './components/Score' does not contain a default export (imported as 'Score')
我知道有很多类似的帖子,但似乎没有一个与这个相似。
使用 react redux 来使用 mapStateToProps。
在我的 App.js 中,我使用import Score from './components/Score';
为了导入组件。
该组件位于名为 Score 的文件夹中,其中包含index.js
和Score.js
。 在Score.js
里面我有:
const Score = ({team_score}) => (
<>
<p>{ team_score }</p>
</>
);
export default Score;
在我的index.js
中,我有:
import { connect } from "react-redux";
import Score from "./Score";
let mapStateToProps = (state) => {
return {
team_score: state.team_score,
};
};
connect(mapStateToProps)(Score)
但我得到了错误:
./src/App.js
Attempted import error: './components/Score' does not contain a default export (imported as 'Score').
我已经三重检查并且文件路径是正确的。 我不确定为什么会收到此错误。
我认为您的问题是您在此文件夹中有index.js
文件。
import Score from './components/Score'
此行将尝试从components/Score/index.js
文件中导入任何内容,并且您不会从那里导出任何内容。
您应该执行以下操作:
import { connect } from "react-redux";
import Score from "./Score";
let mapStateToProps = (state) => {
return {
team_score: state.team_score,
};
};
export default connect(mapStateToProps)(Score)
两种解决方案:
Score
文件夹中的index.js
文件中导出Score
。 (从Score
文件导入后)index.js
并使用import Score from './components/Score/Score';
在App.js
中如果您只从模块返回一个值,则更好的组织方式是1
。
错误原因:您正在从Score
文件夹中导入值。 从文件夹中导入值时,该文件夹中的index.js
会返回该值。 但是这里index.js
没有导出任何值。 因此,要么从index.js
中导出值(解决方案 1),要么从Score
文件夹中的Score
文件中导入值(解决方案 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.