问题
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