为什么在 ES6 模块中导出的对象有未定义的方法?
Why does object exported in ES6 module have undefined methods?
我在 ES6 模块中定义了一个 ES6 class,它导出 class:
的 实例
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.
构造函数中定义的属性确实存在。就好像对象的 prototype 被排除在外,只有它的 members 被导出。
我需要 'babel/register'
.
为什么无法正确导出该对象?
我一问就明白了。看起来 import * as foo from 'module'
和 import foo from 'module'
之间有区别。这有效:
import theObject from './mymodule';
所以不是导出错误的问题,而是导入错误。
我在 ES6 模块中定义了一个 ES6 class,它导出 class:
的 实例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.
构造函数中定义的属性确实存在。就好像对象的 prototype 被排除在外,只有它的 members 被导出。
我需要 'babel/register'
.
为什么无法正确导出该对象?
我一问就明白了。看起来 import * as foo from 'module'
和 import foo from 'module'
之间有区别。这有效:
import theObject from './mymodule';
所以不是导出错误的问题,而是导入错误。