Error when exporting class

0

I have two files in the same folder , one called main.js and the other es.js .

The problem is that when trying to import a class of es.js , from main.js throws me the error:

  

Uncaught SyntaxError: Unexpected token import

main.js:

    import ES from './es.js';

en.js

   class ES{
    constructor(){
    get(symbol, e){
    let d = document;
    return symbol === "#" ? d.getElementById(e) : symbol === "." ? d.getElementsByClassName(e) : d.getElementsByTagName(e);
    }   
    }
}

export default ES;
    
asked by Eduardo Sebastian 05.11.2017 в 07:11
source

1 answer

5

The reason is that the browser you are using to execute that code does not yet support ES modules. You can see a compatibility table in Caniuse .

There are tools that are used a lot when programmed in ES6, ES7, ES8 or even with experimental APIs and / or in development. One of these tools is Babel .

This tool allows you, through presets and plugins , to transpile a code written in ES6 + to an ES5 ( understandable by all browsers), you can also use other tools such as Gulp or Webpack together with Babel to do this process automatically and also generate a bundle , which is simply the code already transpiled, concatenated, optimized and minified.

    
answered by 05.11.2017 в 16:05