为什么在ES6模块中导出的对象有未定义的方法?
我在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 * as foo from 'module'
import foo from 'module'
之间的区别。 这工作:
import theObject from './mymodule';
所以这不是出口错误的问题,而是错误的导入。