gpt4 book ai didi

node.js - 为什么在 ES6 模块中导出的对象有未定义的方法?

转载 作者:搜寻专家 更新时间:2023-10-31 22:40:58 25 4
gpt4 key购买 nike

我在 ES6 模块中定义了一个 ES6 类,它导出该类的一个实例:

class MyObject {
constructor() {
this.propertyA = 1;
this.propertyB = 2;
}

myMethod() {
doStuff();
}
}

var theInstance = new MyObject();
export default theInstance;

当我导入这个模块时,myMethodundefined:

import * as theObject from './my/module';

theObject.myMethod(); // Error! undefined is not a method.

构造函数中定义的属性确实存在。就好像对象的原型(prototype)被排除了,只有它的成员被导出了。

我需要'babel/register'

为什么导出此对象无法正常工作?

最佳答案

我一问就明白了。看起来 import * as foo from 'module'import foo from 'module' 是有区别的。这有效:

import theObject from './mymodule';

所以这不是导出错误的问题,而是导入不正确。

关于node.js - 为什么在 ES6 模块中导出的对象有未定义的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31600002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com