这个是在Github 看到的评论,感觉写的不错,就直接搬过来了
比较
export default
和 import x = require('')
区别
1 2 3 4 5 6 7 8 9 10
|
export default class Calculator { ' 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
|
import Calculator from "./calculator"; let calc = new Calculator(); 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
|
export = class Calculator { public add(num1, num2) { return num1 + num2; } }
|
import … = require(“module”);
1 2 3 4 5 6 7
|
import Calculator = require("./calculator"); let calc = new Calculator(); console.log(calc.add(2, 2));
|
Notes:
- This syntax is only used when importing a CommonJS module.
1 2 3 4 5 6 7 8 9 10 11
|
export class Calculator { public add(num1, num2) { return num1 + num2; } }
|
import { … } from “module”;
1 2 3 4 5 6 7
|
import { Calculator } from "./calculator"; let calc = new Calculator(); 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.