尝试导出一个类作为模块ES6 babel [英] Trying to export a class as a module ES6 babel

查看:133
本文介绍了尝试导出一个类作为模块ES6 babel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ES6类,我试图导出为一个模块,但是我收到一个错误 SyntaxError:意外的保留字



Car.js

  class Car {
constructor(make){
this.make = make;
this.currentSpeed = 25;
}

printCurrentSpeed(){
console.log(this.make +'正在进行'+ this.currentSpeed +'mph。
}
}

module.exports = Car;

我正在尝试使用这样的汽车模块



main.js

  var Car = require(./ models / Car ); 

let must = new Car('Mustang');
console.log(must.printCurrentSpeed());

我正在使用browserify与babelify转换为ES6

  browserify -t babelify main.js -o public / scripts / bundle.js,

我是正确导出汽车模块,还是我做错了?

解决方案

这样做的答案是我可以使用 babel-node 或使用节点用--harmony标志来运行带有es6功能的脚本。



这里是使用 babel-node server 的一些示例alt / iso同构反应示例,​​演示此方法。


I have an ES6 class which I am trying to export as a module, however I am getting an error SyntaxError: Unexpected reserved word

Car.js

class Car {
    constructor(make) { 
        this.make = make;
        this.currentSpeed = 25;
    }

    printCurrentSpeed(){
          console.log(this.make + ' is going ' + this.currentSpeed + ' mph.');
    }
}

module.exports = Car;

I am trying to use the Car module like this

main.js

var Car = require("./models/Car");

let must = new Car('Mustang');
console.log(must.printCurrentSpeed());

I am using browserify with babelify to transform to ES6

browserify -t babelify main.js -o public/scripts/bundle.js",

Am I exporting the Car module correctly, or am I doing something wrong?

解决方案

The answer to this was that I can use either babel-node or use node with --harmony flag to run the script with es6 features.

Here are some example alt/iso Isomorphic React Examples using babel-node server which demonstrate this approach.

这篇关于尝试导出一个类作为模块ES6 babel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆