简体   繁体   English

尝试导入错误:“App”未从“./App”导出

[英]Attempted import error: 'App' is not exported from './App'

I'm working on a currency converter app, I got the code from code pen ( https://codepen.io/jmean/pen/Oxmgxp ).我正在开发货币转换器应用程序,我从代码笔 ( https://codepen.io/jmean/pen/Oxmgxp ) 获得了代码。 I'm trying to run this code and I get this error: ./src/index.js Attempted import error: 'App' is not exported from './App'.我正在尝试运行此代码,但出现此错误:./src/index.js 尝试导入错误:'App' 未从 './App' 导出。

I've tried adding export default App();我试过添加导出默认 App(); on index.js and also on App.js file but its not working.在 index.js 和 App.js 文件上,但它不起作用。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import {App} from './App';
import * as serviceWorker from './serviceWorker';

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

I expected the code to compile and display on the browser like it does on code pen.我希望代码能够像在代码笔上一样编译并显示在浏览器上。

If it's a default export, you don't need to put the import in curly braces.如果是默认导出,则不需要将导入放在花括号中。

import App from './App';

is what you should write.是你应该写的。

There are two ways to structure the import/export pair.有两种方法可以构建导入/导出对。

Default:默认:

App.js应用程序.js

export default class App { ... }

index.js索引.js

import App from './App';

Named:命名:

App.js应用程序.js

export class App { ... }

index.js索引.js

import { App } from './App';

I had an error importing react on App component, it was a fool problem i was importing in a bad way the react core.我在 App 组件上导入反应时出错,这是一个愚蠢的问题,我以错误的方式导入反应核心。

this compiled but had that error这个编译但有那个错误

import {ComponentName} from "./ComponentName"

this worked:这有效:

import ComponentName from './ComponentName'

it was strange, but hope helps!这很奇怪,但希望有所帮助!

There are two ways to export,有两种导出方式,

Named Export命名导出

With named exports, one can have multiple named exports per file.使用命名导出,一个文件可以有多个命名导出。 Then import the specific exports they want surrounded in braces.然后导入他们想要用大括号括起来的特定导出。 The name of imported module has to be the same as the name of the exported module.导入模块的名称必须与导出模块的名称相同。

// imports
import { MyComponent } from "./MyComponent";

// exports from ./MyComponent.js file
export const MyComponent = () => {}

Default Export默认导出

One can have only one default export per file.每个文件只能有一个默认导出。 When we import we have to specify a name and import like:当我们导入时,我们必须指定一个名称并导入如下:

// import
import MyDefaultComponent from "./MyDefaultExport";

// export
const MyComponent = () => {}
export default MyComponent;

Note: The naming of import is completely independent in default export and we can use any name we like.注意: import 的命名在默认导出中是完全独立的,我们可以使用任何我们喜欢的名称。

What for me worked is to specify whole file name: import { App } from './App.js';我的工作是指定整个文件名: import { App } from './App.js'; or import { App } from './App.jsx';或从 './App.jsx' 导入 { App };

If you have in the end export default, then you don't put {} on import App from './App.js';如果您最终有导出默认值,那么您不要将 {} 放在 import App from './App.js' 上; If you don't have it then you put {}.如果你没有它,那么你把 {}。

List item项目清单

暂无
暂无

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

相关问题 尝试导入错误:“app”未从“firebase/app”导出(导入为“firebase”) - Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase') 升级到 Firebase JS 8.0.0:尝试导入错误:“app”未从“firebase/app”导出(导入为“firebase”) - Upgrade to Firebase JS 8.0.0: Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase') 尝试导入错误:'未从 - Attempted import error:' is not exported from 尝试导入错误:“ApiRequests”未从“./ApiRequests”导出 - Attempted import error: 'ApiRequests' is not exported from './ApiRequests' 尝试导入错误:“Typography”未从“antd”导出 - Attempted import error: 'Typography' is not exported from 'antd' 尝试导入错误:“必需”未从“是的”导出 - Attempted import error : 'required' is not exported from 'yup' 尝试导入错误:“getMoviesList”未从“./actions”导出; 反应错误 redux - Attempted import error: 'getMoviesList' is not exported from './actions'; error in react redux 反应错误“尝试导入错误:'TodoItem'未从'./Todoitem'导出。” - error in react " Attempted import error: 'TodoItem' is not exported from './Todoitem'. " 尝试导入错误:未从“ d3”导出“行为” - Attempted import error: 'behavior' is not exported from 'd3' 尝试导入错误:“Navigate”未从“react-router-dom”导出 - Attempted import error: 'Navigate' is not exported from 'react-router-dom'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM