在使用 export 导出后,import 导入时需要使用花括号对应模块。使用 export default 后,可以省略花括号。
DefaultMyClass.js
class MyClass {}
MyClass.prototype.hello = function() {
console.log("hello");
}
export { MyClass as default };
jsTestDefaultMyClass.html
<script type="module">
import TheClass from "./DefaultMyClass.js";
console.log(TheClass);
let theClass = new TheClass();
theClass.hello();
script>
DefaultMyClass.js
class MyClass {}
MyClass.prototype.hello = function() {
console.log("hello");
}
class NewClass {}
NewClass.prototype.hello = function() {
console.log("new hello");
}
export { MyClass, NewClass as default };
jsTestDefaultMyClass.html
<script type="module">
import TheClass from "./DefaultMyClass.js";
console.log(TheClass);
let theClass = new TheClass();
theClass.hello();
script>
DefaultMyClass.js
class MyClass {}
MyClass.prototype.hello = function() {
console.log("hello");
}
class NewClass {}
NewClass.prototype.hello = function() {
console.log("new hello");
}
export { MyClass, NewClass as default};
jsTestDefaultMyClass.html
<script type="module">
import TheClass, { MyClass } from "./DefaultMyClass.js";
console.log(TheClass);
let theClass = new TheClass();
theClass.hello();
console.log(MyClass);
let myClass = new MyClass();
myClass.hello();
script>
DefaultMyClass.js
class MyClass {}
MyClass.prototype.hello = function() {
console.log("hello");
}
class NewClass {}
NewClass.prototype.hello = function() {
console.log("new hello");
}
class GreyClass {}
GreyClass.prototype.hello = function() {
console.log("grey hello");
}
export { GreyClass, NewClass as default, MyClass};
jsTestDefaultMyClass.html
<script type="module">
import TheClass, { GreyClass as NewGreyClass, default as NewClass, MyClass } from "./DefaultMyClass.js";
console.log(TheClass);
let theClass = new TheClass();
theClass.hello();
console.log(NewGreyClass);
let greyClass = new NewGreyClass();
greyClass.hello();
console.log(NewClass);
let newClass = new NewClass();
newClass.hello();
console.log(MyClass);
let myClass = new MyClass();
myClass.hello();
script>
DefaultMyClass.js
class MyClass {}
MyClass.prototype.hello = function() {
console.log("hello in MyClass");
}
function hello() {
console.log("hello function");
}
var obj = { hello:()=>{ console.log("hello in obj"); } };
var age = 18;
export default { MyClass, hello, obj, age};
jsTestDefaultMyClass.html
<script type="module">
import myModules from "./DefaultMyClass.js";
console.log(myModules);
let myClass = new myModules.MyClass();
myClass.hello();
myModules.hello();
myModules.obj.hello();
console.log("age=",myModules.age);
script>
https://www.cnblogs.com/libin-1/p/7127481.html