简体   繁体   English

电子ES6代码意外的令牌导入

[英]electron ES6 code Unexpected token import

Development electron app. 开发电子应用程序。

use zhe ES6 code in html is success. 在HTML中使用这些ES6代码是成功的。 but write ES6 code in alone js fail is error. 但是用单独的js编写ES6代码失败是错误的。

package.json: package.json:

{
  "name": "app",
  "version": "0.1.0",
  "main": "main.js",
  "dependencies": {
    "react": "^15.3.0",
    "react-dom": "^15.3.0"
  }
}

index.html in electron is success 电子中的index.html成功

<head>
    <meta charset="UTF-8">
    <title>STW</title>
    <script src="js/browser.min.js"></script>
</head>

<body>
<div id="container">
</div>
<script type="text/babel">
    import React,{Component} from 'react';
    import ReactDOM from 'react-dom';
//    import FF from'./Foo.js';
    class F1 extends Component{
        render(){
            return <div>This is React component</div>
        }
    };
    ReactDOM.render(<F1/>,document.getElementById("container"))


</script>
</body>
</html>

when i use import FF from'./Foo.js'; 当我import FF from'./Foo.js';使用import FF from'./Foo.js';import FF from'./Foo.js'; it's error and show "Unexpected token import". 错误,并显示“意外的令牌导入”。

Foo.js Foo.js

import React from 'react'

export default class Foo extends React.Component{
    render(){
        return <div>This is React  component</div>
    }
}

electron is latest version. 电子版是最新版本。

ES6 code in html <script type="text/babel"> is success, How can i use zhe ES6 code in alone file ? html <script type="text/babel">中的ES6代码成功,如何在单独的文件中使用ES6代码? thanks =.= 谢谢=。=

As for my knowledge, support for ES6 modules first needs to land in V8. 据我所知,首先需要在V8中获得对ES6模块的支持。 For now you have to use: 现在,您必须使用:

const ReactDOM = require('react-dom')

You are missing a space (' ') in this line: 您在此行中缺少空格(''):

import FF from'./Foo.js';

Should be: 应该:

import FF from './Foo.js';

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

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