尝试导出一个类作为模块ES6 babel [英] Trying to export a class as a module ES6 babel
问题描述
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屋!