javascript - 错误 : *. default is not a constructor

标签 javascript typescript ava

在测试从 typescript 文件转译的一些 javascript 代码时,出现以下错误。

这里是错误:

Error: _mapAction2.default is not a constructor

这是导致错误的代码行:

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

这是原始的 typescript 文件 ma​​p-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 文件 ma​​p-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

最佳答案

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

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';

关于javascript - 错误 : *. default is not a constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42652423/

相关文章:

javascript - Owl Carousel 2 - 单击幻灯片时拖动事件

typescript - Angular 5 Modal以编程方式关闭

javascript - typescript :定义没有特定键和链对象的接口(interface)

angular - 为什么我的变量没有在 Typescript 中被指定为数组?

node.js - 如何为 NodeJS 单元测试模拟 request-promise-native?

ava - 针对数据库的集成测试 - AVA

node.js - Firebase Admin : admin. auth().delete(uid) 未在 AVA test.after() 中解析

Javascript:将非常大和非常小的数字转换为固定的人类可读字符串

javascript - 何时使用 attrs 与直接通过样式组件传递 props ?

javascript - 有没有办法在 `flatpickr` 的 onChange 事件中区分时间选择事件和日历事件?