我在ES6模块中定义了一个ES6类,该类导出该类的实例:
class MyObject {
constructor() {
this.propertyA = 1;
this.propertyB = 2;
}
myMethod() {
doStuff();
}
}
var theInstance = new MyObject();
export default theInstance;
当我导入此模块时,myMethod
是undefined
:
import * as theObject from './my/module';
theObject.myMethod(); // Error! undefined is not a method.
构造函数中定义的属性确实存在。好像对象的原型被排除在外,只有其成员被导出。
我需要'babel/register'
。
为什么导出该对象不能正常工作?
我问了之后马上就弄明白了。似乎与import * as foo from 'module'
和之间存在差异import foo from 'module'
。这有效:
import theObject from './mymodule';
因此,导出错误的东西不是问题,而是错误地导入了它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句