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

当测试从打印脚本文件中传输的一些 javascript 代码时,我得到了以下错误。

这里有一个错误:

Error: _mapAction2.default is not a constructor

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

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

下面是原始打印文件 地图行动:

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-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
118645 次浏览

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

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

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

import LatLngLiteral from '';

to

import { LatLngLiteral } from '';

Another solution would be to add "esModuleInterop": true, into tsconfig.json.

esModuleInterop allows default imports from modules with no default export.

I faced this issue when I was using node JS and mongoDB. the issue was on the way I imported the User model.

This is the way worked for me

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

If you had tried the answers here and they didn't help then verify the stack trace of your error carefully - I found the circular dependency in my code (but the error was the same as in this question so the cause was absolutely not evident)