亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

node.js中使用Export和Import的方法

 更新時(shí)間:2017年09月18日 11:30:21   作者:鐘偉V5  
這篇文章主要介紹了node.js中使用Export和Import的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

Nodejs 6.x版本還沒有支持export 和import

import與export是es6中模塊化的導(dǎo)入與導(dǎo)出,node.js現(xiàn)階段不支持,需要通過babel進(jìn)行編譯,使其變成node.js的模塊化代碼。(關(guān)于node.js模塊,可參考其他node.js模塊化的文章)

繼續(xù)使用exports和require

test.js

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  add (){
    this.x = this.x ? this.x : 1;
    this.y = this.y ? this.y : 2;
    return this.x + this.y;
  }
}
const PI = 3.1415926;
exports.Point = Point;
exports.PI = PI;
let a = new Point();
console.log(a.add());

test2.js

let {Point,PI} = require('./test.js');
class Point2 extends Point{

  //add();
}

let b = new Point2();
console.log(b.add());
console.log(PI);

使用babel來支持export 和 import

在package.json增加 babel的配置

 "babel": {
  "presets": ["es2015"]
 },
 "scripts": {
  "start": "node app/51job.js",
  "build": "babel src -d app"
 },

安裝babel相關(guān)模塊

npm install --save-dev babel-cli
npm install babel-preset-es2015 --save-dev

在命令行使用 npm run build 就可以把src目錄下的所有javascript文件轉(zhuǎn)換為標(biāo)準(zhǔn)javascript代碼到lib目錄。async和await都可以使用了。

 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評論