简体   繁体   English

ES6完整项目中的模块

[英]Modules in ES6 full project

It's 2016 ES6 is released and Chrome Canary has 93% support for the new ECMA-Script. 它于2016年发布了ES6,Chrome Canary对新的ECMA-Script具有93%的支持。

So I tried to use Modules in the ES6 way. 因此,我尝试以ES6方式使用模块。

Like: 喜欢:

( http://exploringjs.com/es6/ch_modules.html#_default-exports-one-per-module ) http://exploringjs.com/es6/ch_modules.html#_default-exports-one-per-module

    Or a class:

//------ MyClass.js ------
export default class { ··· } // no semicolon!

//------ main2.js ------
import MyClass from 'MyClass';
const inst = new MyClass();

To play around i made an index.html file and two files like above described with some simple content for MyClass like: 为了玩转,我制作了一个index.html文件和两个上述文件,并为MyClass提供了一些简单的内容,例如:

constructor(name) {
    this.name = name;
    this.currentSpeed = 25;
}

so i have: 所以我有:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <module src="MyClass.js"></module>
    <script type="text/javascript" src="main2.js"></script>
</head>
<body>
<p>Hallo:)</p>
</body>
</html>

in index.html 在index.html中

    export default class {
    constructor(name) {
    this.name = name;
    this.currentSpeed = 25;
}}

in MyClass.js and 在MyClass.js和

import MyClass from 'MyClass';
const inst = new MyClass();

in main2.js 在main2.js中

this isn't working for me. 这对我不起作用。 Everytime an error for the import in main2.js. 每次在main2.js中导入错误。 Is somebody capable to help me? 有人可以帮助我吗?

Two things here: 这里有两件事:

  1. Browsers are not actually shipping ECMAScript module support just yet. 浏览器尚未真正提供ECMAScript模块支持。 I imagine it will be all over Twitter by the time they do. 我想象到他们这样做的时候,它会遍及整个Twitter。
  2. You will need to use <script type=module src=modulescript.js> to use module scripts in HTML. 您将需要使用<script type=module src=modulescript.js>来使用HTML中的模块脚本。 (There's also new Worker("moduleworker.js", {type:"module"}) for workers, but again, not supported just yet.) (还为new Worker("moduleworker.js", {type:"module"}) ,但同样,目前尚不支持。)

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

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