简体   繁体   中英

Modules in ES6 full project

It's 2016 ES6 is released and Chrome Canary has 93% support for the new ECMA-Script.

So I tried to use Modules in the ES6 way.

Like:

( 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:

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

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

in MyClass.js and

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

in main2.js

this isn't working for me. Everytime an error for the import in main2.js. Is somebody capable to help me?

Two things here:

  1. Browsers are not actually shipping ECMAScript module support just yet. I imagine it will be all over Twitter by the time they do.
  2. You will need to use <script type=module src=modulescript.js> to use module scripts in HTML. (There's also new Worker("moduleworker.js", {type:"module"}) for workers, but again, not supported just yet.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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