2017-03-07 115 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