简体   繁体   English

es6导出引发解析错误:意外令牌

[英]es6 export throws Parsing error: Unexpected token

App.js: App.js:

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

index.js: index.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'));

(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
  1. You get an error because you have invalid export syntax. 由于错误的导出语法,您将收到一个错误。 Try export class App instead. 请尝试export class App
  2. When you write 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 ;
  3. 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. 这意味着当您在import语句中未指定名称时,将导入默认实体。
// App.js
export default App;

// other_module.js
import App from App.js // import default from App.js and put it into 'App' var
  1. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM