JS中的进出口类
当我尝试在JS中导入类时,我有问题:
//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);
在所有替代方案中,我都有相同的错误:
未接收参考:在初始化之前无法访问词汇声明“ myclass” http://127.0.0.1:5500/js/file2.js
in html:
<script type="module" src="./js/file1.js"></script>
<script type="module" src="./js/file2.js"></script>
I have a problem when I try to import class in js:
//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);
In all alternatives I have the same error:
Uncaught ReferenceError: can't access lexical declaration 'myClass' before initialization
http://127.0.0.1:5500/js/file2.js
In the html :
<script type="module" src="./js/file1.js"></script>
<script type="module" src="./js/file2.js"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)