js 中的导入导出 class
Import-Export class in js
我尝试在 js 中导入 class 时遇到问题:
//file 1
export default class myClass {
constructor(){
this.name = "name1";
}
saysHello(){
console.log("hello"+this.name1);
}
};
//file 2
import { myClass } from 'file1.js'
//I have tried different alternatives
// alternative 1:
var v1 = new myClass();
v1.saysHello()
//alternative 2:
myClass.saysHello()
//alternative 2:
var v1 = Object.create(myClass);
在所有替代方案中我都有同样的错误:
Uncaught ReferenceError: can't access lexical declaration 'myClass' before initialization
http://127.0.0.1:5500/js/file2.js
在html中:
<script type="module" src="./js/file1.js"></script>
<script type="module" src="./js/file2.js"></script>
//file 2
import myClass from './file-1.js'
//I have tried different alternatives
// alternative 1:
var v1 = new myClass();
v1.saysHello()
我尝试在 js 中导入 class 时遇到问题:
//file 1
export default class myClass {
constructor(){
this.name = "name1";
}
saysHello(){
console.log("hello"+this.name1);
}
};
//file 2
import { myClass } from 'file1.js'
//I have tried different alternatives
// alternative 1:
var v1 = new myClass();
v1.saysHello()
//alternative 2:
myClass.saysHello()
//alternative 2:
var v1 = Object.create(myClass);
在所有替代方案中我都有同样的错误:
Uncaught ReferenceError: can't access lexical declaration 'myClass' before initialization http://127.0.0.1:5500/js/file2.js
在html中:
<script type="module" src="./js/file1.js"></script>
<script type="module" src="./js/file2.js"></script>
//file 2
import myClass from './file-1.js'
//I have tried different alternatives
// alternative 1:
var v1 = new myClass();
v1.saysHello()