这个是在Github 看到的评论,感觉写的不错,就直接搬过来了

比较

export defaultimport x = require('')区别

export default … (Default Export)

1
2
3
4
5
6
7
8
9
10
// calculator.ts                                // compiled.js
// ============= // ===========
export default class Calculator { // var Calculator = /** @class */ (function () {'
' public add(num1, num2) { // function Calculator() {}
return num1 + num2; // Calculator.prototype.add = function (num1, num2) {
} // return num1 + num2;
} // };
// return Calculator;
// }());
// exports["default"] = Calculator;

import … from “module”;

1
2
3
4
5
6
7
// importer.ts                                  // compiled.js
// =========== // ===========
import Calculator from "./calculator"; // exports.__esModule = true;
// var calculator = require("./calculator");
let calc = new Calculator(); // var calc = new calculator["default"]();
// console.log(calc.add(2, 2));
console.log(calc.add(2, 2)); //
Notes:
  • A default export can be imported with any name.
  • Functionally equivalent to import * as Calculator from "./calculator"; and then instantiating it using new Calculator.default().

export = …

1
2
3
4
5
6
7
8
9
// calculator.ts                                // compiled.js
// ============= // ===========
export = class Calculator { // module.exports = /** @class */ (function () {
public add(num1, num2) { // function Calculator() {}
return num1 + num2; // Calculator.prototype.add = function (num1, num2) {
} // return num1 + num2;
} // };
// return Calculator;
// }());

import … = require(“module”);

1
2
3
4
5
6
7
// importer.ts                                  // compiled.js
// =========== // ===========
import Calculator = require("./calculator"); // exports.__esModule = true;
// var Calculator = require("./calculator");
let calc = new Calculator(); // var calc = new Calculator();
// console.log(calc.add(2, 2));
console.log(calc.add(2, 2)); //
Notes:
  • This syntax is only used when importing a CommonJS module.

export … (Named Export)

1
2
3
4
5
6
7
8
9
10
11
// calculator.ts                                // compiled.js
// ============= // ===========
export class Calculator { // exports.__esModule = true;
public add(num1, num2) { // var Calculator = /** @class */ (function () {
return num1 + num2; // function Calculator() {}
} // Calculator.prototype.add = function (num1, num2) {
} // return num1 + num2;
// };
// return Calculator;
// }());
// exports.Calculator = Calculator;

import { … } from “module”;

1
2
3
4
5
6
7
// importer.ts                                  // compiled.js
// =========== // ===========
import { Calculator } from "./calculator"; // exports.__esModule = true;
// var calculator = require("./calculator");
let calc = new Calculator(); // var calc = new calculator.Calculator();
// console.log(calc.add(2, 2));
console.log(calc.add(2, 2)); //
Notes:
  • Named exports are useful to export several values.
  • During the import, you must use the same name of the corresponding object.