Javascript 错误:*.default 不是构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42652423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: *.default is not a constructor
提问by ChristopherMortensen
I get the following error, when testing some javascript code, transpiled from a typescript file.
在测试一些从打字稿文件转译的 javascript 代码时,我收到以下错误。
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:
这是原始的打字稿文件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:
这是转换后的 .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
回答by euvl
You need to export a defaultvalue 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 multiplethings 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';
回答by Datsos
Check that your import is correct. you might miss {} for example.
检查您的导入是否正确。例如,您可能会错过 {}。
import LatLngLiteral from '';
to
到
import { LatLngLiteral } from '';
回答by arshbot
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
由于 javascript中的类是语法糖,我想我会尝试在没有它们的情况下解决这个问题。对我来说,将架构切换到原型似乎解决了我的问题。发布以防其他人遇到此问题但已经在做export default

