2017-03-07 116 views
18

当测试一些JavaScript代码时,从打字稿文件转译出来时出现以下错误。错误:* .default不是构造函数

我该怎么做才能解决这个问题?

以下是错误:

Error: _mapAction2.default is not a constructor 

这里是导致错误的代码行:

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

这里原来打字稿文件地图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; 
    } 

} 

这里是transpiled的.js文件地图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 

提前感谢!

回答

25

您需要导出默认值,这将是这样的:

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

或替代,进口'{}地图行动从” /无出口默认 – Ksyqo

+0

map_action_file''谢谢YEV,我合并所有MAP_ACTION相关文件合并成一个,然后@ Ksygo的评论修正了错误! – ChristopherMortensen

+1

缺乏{}让我如此:从'./Frak'导入{Frak};'修复了它。谢谢。 – RyanNerd