[英]es6 export throws Parsing error: Unexpected token
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
};
export App
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'));
(1)When I run the the above code I am getting the below error at last line of App.js ie export statement (1)当我运行上面的代码时,我在App.js的最后一行即导出语句中得到以下错误
"parsing error:unexpected token"
The error wouldnt go away even when imported the module as 即使将模块导入为
import { App } from './App';
I am aware that we can fix this error by writing 我知道我们可以通过编写此错误来解决
export default App
instead of 代替
export App
it also works if we just append export to App as below 如果我们仅将导出追加到App,如下所示
export class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
};
I am not able to figure out this behavior of export statement .I googled but they are only confusing me more. 我无法弄清楚export statement的行为。我用google搜索,但它们只会使我更加困惑。
2)Also whats the difference between 2)还有什么区别
export App
vs 与
export const App
export class App
instead. export class App
。 export class App
, it means that you export a class with a name 'App' from this module. export class App
,这意味着您将从此模块中导出名称为“ App”的类。 It could be imported then by its name like so: import { App } from App.js
; import { App } from App.js
; export default
makes passed entity a default exported entity. export default
使传递的实体成为默认的导出实体。 It means that when you don't specify a name in import statement, default entity will be imported. // App.js
export default App;
// other_module.js
import App from App.js // import default from App.js and put it into 'App' var
export const App
means that App won't change in the module from which it was imported. export const App
表示应用程序不会在从其导入的模块中更改。 export App
is invalid syntax, take a look at export let
for clarification on what's the difference. export App
的语法无效,请看一下export let
澄清它们之间的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.