Error: *.default is not a constructor

妖精的绣舞 提交于 2019-12-04 14:59:59

问题


I get the following error, when testing some javascript code, transpiled from a typescript file.

Here is the error:

Error: _mapAction2.default is not a constructor

Here is the line of code that caused the error:

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

Here is the original typescript-file 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;
    }

}

Here is the transpiled .js-file 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

回答1:


You need to export a default value which will look like:

export default class MapAction implements IMapAction {...

And import it as:

import MapAction from './map_action_file';

Alternatively, if you want to export multiple things from the module you can do something like:

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

And import it as follows:

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



回答2:


Check that your import is correct. you might miss {} for example.

import LatLngLiteral from '';

to

import { LatLngLiteral } from '';



回答3:


Since classes in javascript are syntactic sugar, I figured I'd try to solve this issue without them. For me, switching the architecture over to prototypes seems to have solved my issue. Posting in case anyone else runs into this issue but is already doing export default



来源:https://stackoverflow.com/questions/42652423/error-default-is-not-a-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!