简体   繁体   English

尝试导入错误:“./components”不包含默认导出(导入为“App”)

[英]Attempted import error: './components' does not contain a default export (imported as 'App')

I have the following ReactJS project structure and I'm getting the following error:我有以下 ReactJS 项目结构,但出现以下错误:

./src/index.js ./src/index.js

Attempted import error: './components' does not contain a default export (imported as 'App').尝试导入错误:“./components”不包含默认导出(导入为“App”)。

My goal is to import components like this: import { App, Navbar } from 'components';我的目标是像这样导入组件: import { App, Navbar } from 'components'; (notice the 'components') and not like ./components/App , ./components/App/index or so. (注意“组件”)而不像./components/App./components/App/index等等。 To do that, I needed to add index.js in the components directory.为此,我需要在组件目录中添加 index.js。 I tried doing that by the following code, but I'm receiving the error above.我尝试通过以下代码执行此操作,但收到上述错误。

What's the reason?什么原因? How do I solve it?我该如何解决?

There are similar threads, but I'm already exporting it by export default App;有类似的线程,但我已经通过export default App; in./components/App/index.jsx.在./components/App/index.jsx。 Maybe the reason is the.jsx extension?也许原因是 .jsx 扩展名?

在此处输入图像描述

  • index.js索引.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './components';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));

serviceWorker.unregister();

  • components/index.js组件/index.js
export App from './App';
  • components/App/index.jsx组件/App/index.jsx
import React, { Fragment } from 'react';
import './style.css';
import { BrowserRouter as Router, Switch, Route, Redirect } from 'react-router-dom';
import NotAuthorizedRoute from '../../common/NotAuthorizedRoute';
import { Navbar, Home, User, Login } from 'components';

const App = () => {
  return (
    <Fragment>
      <Router>
        <Navbar />

        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/users" component={User} />
          <NotAuthorizedRoute path="/sign-in" component={Login} />
          <Redirect from="*" to="/" />
        </Switch>

      </Router>
    </Fragment>
  );
};

export default App;

  • components/App/style.css组件/App/style.css

What I tried:我尝试了什么:

I tried doing the following, like people said in this thread: Attempted import error: 'App' is not exported from './App' .我尝试执行以下操作,就像人们在此线程中所说的那样: Attempted import error: 'App' is not exported from './App'

  • components/index.js组件/index.js
export { App } from './App'; // note the brackets

But then I got:但后来我得到了:

./src/components/index.js
Attempted import error: 'App' is not exported from './App'.

It seems you are having some confusions with ES6 export / import syntaxes.看来您对 ES6 导出/ 导入语法有些困惑。 MDN documentation is very explicative, I would recommend you to check it. MDN 文档非常详细,我建议您查看一下。

The most important thing is to remember the difference between default and named export/import.最重要的是记住默认和命名导出/导入之间的区别。 (HINT: When you see brackets in export/import statements you are dealing with named ones) (提示:当您在导出/导入语句中看到括号时,您正在处理命名语句)

In your specific case, you will have to do as follows:在您的具体情况下,您必须执行以下操作:

  • ./components/component-a.jsx ./components/component-a.jsx
const ComponentA = () => {
  return (
    <MyComponent/>
  );
};

export {ComponentA};
  • ./components/component-b.jsx ./components/component-b.jsx
const ComponentB = () => {
  return (
    <MyComponent/>
  );
};

export {ComponentB};
  • ./components/index.js ./components/index.js
import {componentA} from './component-a.jsx';
import {componentB} from './component-b.jsx';

export {componentA, componentB};
  • ./some-file.jsx ./some-file.jsx
import {componentA, componentB} from './components';
  • ./some-other-file.jsx ./some-other-file.jsx
import {componentA} from './components';

This is the typical pattern to create a virtual export/import namespace for logical module classification.这是为逻辑模块分类创建虚拟导出/导入命名空间的典型模式。

Please note that I've not used default exports.请注意,我没有使用默认导出。 There are several opinions about this, but I would recommend to not use them to avoid errors like the one you are having.对此有几种意见,但我建议不要使用它们以避免像您所遇到的错误。

Please also note that you'll always have to specify the full relative path to the components directory.另请注意,您始终必须指定components目录的完整相对路径。 The only way to import things from 'components' is by having a components package installed in your node_modules directory.'components'导入内容的唯一方法是在node_modules目录中安装components package。

Make sure you are not using the exported value componentA in {componentA}确保您没有在 {componentA} 中使用导出值 componentA

remove `{}` and check

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React-Redux:尝试导入错误:“./components/Score”不包含默认导出(导入为“Score”) - React-Redux: Attempted import error: './components/Score' does not contain a default export (imported as 'Score') 尝试导入错误:“./components/headerComponent/header”不包含默认导出(导入为“标题”) - Attempted import error: './components/headerComponent/header' does not contain a default export (imported as 'Header') 尝试导入错误:“./movieReducer”不包含默认导出(导入为“movieReducer”) - Attempted import error: './movieReducer' does not contain a default export (imported as 'movieReducer') index.js 尝试导入错误:“./App”不包含默认导出(导入为“App”) - index.js attempted import error: './App' does not contain a default export (imported as 'App') ./src/index.js 尝试导入错误:“./App.js”不包含默认导出(导入为“App”) - ./src/index.js Attempted import error: './App.js' does not contain a default export (imported as 'App') 尝试导入错误:“firebase/app”不包含默认导出(导入为“firebase”) - Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase') ./src/components/styles.js 尝试导入错误:&#39;@material-ui/core/styles&#39; 不包含默认导出(作为 &#39;makeStyles&#39; 导入) - ./src/components/styles.js Attempted import error: '@material-ui/core/styles' does not contain a default export (imported as 'makeStyles') 导入错误:“./App”不包含默认导出(导入为“App”) - import error: './App' does not contain a default export (imported as 'App') [React.js] 尝试导入错误:&#39;./registerServiceWorker&#39; 不包含默认导出(导入为 &#39;registerServiceWorker&#39;) - [React.js]Attempted import error: './registerServiceWorker' does not contain a default export (imported as 'registerServiceWorker') 尝试导入错误:&#39;path-to-regexp&#39; 不包含默认导出(导入为 &#39;pathToRegexp&#39;) - Attempted import error: 'path-to-regexp' does not contain a default export (imported as 'pathToRegexp')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM