typescript 错误:*.default不是构造函数

uyto3xhc  于 2023-02-05  发布在  TypeScript
关注(0)|答案(7)|浏览(158)

我得到了下面的错误,当测试一些javascript代码,从一个typescript文件。
下面是错误:

Error: _mapAction2.default is not a constructor

下面是导致错误的代码行:

var mapAction = new MapAction(MapActionType.POLYGONDRAGGED, []);

下面是原始的打印脚本文件map-action. ts

import { IMapAction} from './imap-action';
import { MapActionType } from './map-action-type.enum';
import {LatLngLiteral} from 'angular2-google-maps/core';

export class MapAction implements IMapAction{
    type: MapActionType;
    paths: Array<LatLngLiteral>;

    constructor(mapActionType: MapActionType, paths: Array<LatLngLiteral>){
        this.type = mapActionType;
        this.paths = paths;
    }

    public getType(): MapActionType{
        return this.type;
    }

    public getPaths(): Array<LatLngLiteral>
    {
        return this.paths;
    }

}

下面是翻译的. js文件map-action. js

"use strict";
class MapAction {
    constructor(mapActionType, paths) {
        this.type = mapActionType;
        this.paths = paths;
    }
    getType() {
        return this.type;
    }
    getPaths() {
        return this.paths;
    }
}
exports.MapAction = MapAction;
//# sourceMappingURL=map-action.js.map
vuktfyat

vuktfyat1#

您需要导出一个默认值,如下所示:

export default class MapAction implements IMapAction {...

并将其导入为:

import MapAction from './map_action_file';

或者,如果要从模块导出多个内容,可以执行以下操作:

export class MapAction ...
export class MapSomethng ...

并按如下方式导入:

import { MapAction, MapSomething } from './map_action_file';
q3qa4bjr

q3qa4bjr2#

另一个解决方案是将"esModuleInterop": true,添加到tsconfig.json中。
esModuleInterop允许从没有默认导出的模块默认导入。

relj7zay

relj7zay3#

请检查导入是否正确。例如,您可能会丢失{}。

import LatLngLiteral from '';

import { LatLngLiteral } from '';
bqjvbblv

bqjvbblv4#

我在使用node JS和mongoDB时遇到过这个问题。问题出在我导入用户模型的方式上。
这就是我的工作方式

import { User } from '../models/User'
cnjp1d6j

cnjp1d6j5#

如果你已经尝试了这里的答案,他们没有帮助,然后仔细验证你的错误堆栈跟踪-我发现我的代码中的循环依赖(但错误是相同的,所以在这个问题的原因是绝对不明显)

yacmzcpb

yacmzcpb6#

我最近遇到了这个问题,我的导入是正确的,我不得不编译我的TypeScript服务,因为我的ts-class的js-class有不同的代码。
I did this in IntelliJ

efzxgjgh

efzxgjgh7#

由于javascript are syntactic sugar中有类,我想我可以尝试不用它们来解决这个问题。对我来说,将架构切换到原型似乎已经解决了我的问题。如果其他人遇到这个问题,但已经在使用export default,请发布

相关问题