2016-03-25 97 views
1

我/在此模塊中的文件夾模式稱爲區area_getter.ts:導出/導入模塊不能正常工作打字稿

export module Area { 
    interface iGetAreas { 
     _areasList: Array <any> ; 
     _areas: KnockoutObservableArray <string> ; 
     getAreas(geonameId: string): void; 
    } 



    export abstract class AreaGetter implements iGetAreas { 
     _areasList: Array <any> = []; 
     _areas = ko.observableArray([]); 
     _selectedAreaName: KnockoutObservable <string> ; 
     _selectedAreaGeonameId: KnockoutObservable <string> ; 
     _type: Area.Type; 

     constructor(type: Area.Type) { 
      this._type = type; 
      this._selectedAreaGeonameId = ko.observable(''); 
      this._selectedAreaName = ko.observable(''); 
      this._selectedAreaName.subscribe((newValue) => { 
       console.log(newValue); 
       console.log(this._selectedAreaGeonameId() + "  " + this._selectedAreaName()); 
      }); 
     } 

     getAreas = (geonameId: any): void => { 
      this.showLoadingIcon(); 
      this._areasList = []; 
      $.ajax({ 
       url: `http://api.geonames.org/children?geonameId=${geonameId}&username=elion` 
      }).then((allAreasXML) => { 
       this.hideLoadingIcon(); 
       var allAreasJSON = xml2json(allAreasXML); 
       var allAreas = JSON.parse(allAreasJSON); 
       if (allAreas.geonames.length) { 
        for (var index = 1; index < allAreas.geonames.length - 1; index++) { 
         this._areasList.push(allAreas.geonames[index].geoname); 
        } 
       } else { 
        if (allAreas.geonames.geoname) { 
         this._areasList.push(allAreas.geonames.geoname); 
        } else { 
         this._areasList.push({ 
          name: 'Any', 
          geonameId: 0 
         }); 
        } 
       } 
       this._areas(this._areasList); 

       if (this._type === Area.Type.Region) { 
        this._initiateRegionSelect(); 
       } 
      }); 
     } 

      _initiateRegionSelect =() => { 
      $('#region-select').multiselect({ 
       buttonWidth: '100%', 
       buttonContainer: '<div style="height: 64px;" />', 
       buttonClass: 'none', 
       nonSelectedText: "Select Region", 
       onChange: (option, checked) => { 
        var values = []; 
        $('#region-select option').each(function() { 
         if ($(this).val() !== option.val()) { 
          values.push($(this).val()); 
         } 
        }); 
        $('#region-select').multiselect('deselect', values); 
        $('#region-select').next().removeClass("open").addClass("closed"); 
        this._selectedAreaGeonameId(option.val()); 
        this._selectedAreaName(option.text()); 
       } 
      }); 
     } 

      showLoadingIcon =() => { 
      if (this._type === Area.Type.Town) { 
       $("#town-div span.multiselect-selected-text").css("visibility", "hidden"); 
       $("#town-icon").addClass('special'); 
       $("#town-icon").css("background", "url(assets/gifs/ajax-loader.gif) no-repeat"); 
       $("#town-icon").css("background-size", "100%"); 
      } else if (this._type === Area.Type.Region) { 
       $("#region-div span.multiselect-selected-text").css("visibility", "hidden"); 
       $("#region-icon").addClass('special'); 
       $("#region-icon").css("background", "url(assets/gifs/ajax-loader.gif) no-repeat"); 
       $("#region-icon").css("background-size", "100%"); 
      } else if (this._type === Area.Type.Suburb) { 
       $("#suburb-div span.multiselect-selected-text").css("visibility", "hidden"); 
       $("#suburb-icon").addClass('special'); 
       $("#suburb-icon").css("background", "url(assets/gifs/ajax-loader.gif) no-repeat"); 
       $("#suburb-icon").css("background-size", "100%"); 
      } 
     } 

      hideLoadingIcon =() => { 
      if (this._type === Area.Type.Town) { 
       $("#town-div span.multiselect-selected-text").css("visibility", "visible"); 
       $("#town-icon").removeClass('special'); 
       $("#town-icon").css("background", "transparent"); 
       $("#town-icon").css("background-size", "100%"); 
      } else if (this._type === Area.Type.Region) { 
       $("#region-div span.multiselect-selected-text").css("visibility", "visible"); 
       $("#region-icon").removeClass('special'); 
       $("#region-icon").css("background", "transparent"); 
       $("#region-icon").css("background-size", "100%"); 
      } else if (this._type === Area.Type.Suburb) { 
       $("#suburb-div span.multiselect-selected-text").css("visibility", "visible"); 
       $("#suburb-icon").removeClass('special'); 
       $("#suburb-icon").css("background", "transparent"); 
       $("#suburb-icon").css("background-size", "100%"); 
      } 
     } 
    } 

    export class MultipleSelectAreaGetter extends AreaGetter { 
     _selectedAreas = ko.observableArray([]); 
    } 

    export class SingleSelectAreaGetter extends AreaGetter { 



    } 

    export enum Type { 
     Region, 
     Town, 
     Suburb 
    } 
} 

後來,當我嘗試view_models/search_view_model.ts使用它:

class SearchFilterViewModel { 
    _regionGetter: Area.SingleSelectAreaGetter; 
    _townGetter: SingleSelectAreaGetter; 
    _suburbGetter: MultipleSelectAreaGetter; 
    _categories: KnockoutObservableArray<Category>; 
    _selectedCategories: KnockoutObservableArray<Category>; 

    constructor() { 
     this._categories = ko.observableArray([ 
      new Category("Vegan Meat", 1), 
      new Category("Vegan Dairy", 2), 
      new Category("Confectionary", 3), 
      new Category("Baking", 4), 
      new Category("Dessert", 5), 
      new Category("Restaurants", 6), 
      new Category("Grocers", 7) 
     ]); 
     this._selectedCategories = ko.observableArray([]); 
     this._regionGetter = new Area.SingleSelectAreaGetter(Area.Type.Region); 
     this._townGetter = new SingleSelectAreaGetter(Type.Town); 
     this._suburbGetter = new MultipleSelectAreaGetter(Type.Suburb); 
     this._regionGetter.getAreas("2186224"); 
     //this._regionGetter._selectedAreaGeonameId.subscribe(this._townGetter.getAreas.bind(this._townGetter)); 
     //this._townGetter._selectedArea.subscribe(this._suburbGetter.getAreas.bind(this._suburbGetter)); 
    } 
} 

它不識別「區域」。

如何讓search_view_model識別Area模塊?

我會做類似import { Area } from "./models/area_getter";嗎?

這在進口聲明說

cannot find module Area

這裏的錯誤是我的文件夾結構:

enter image description here

tsconfig.json:

{ 
    "compileOnSave": true, 
    "compilerOptions": { 
     "target": "es5", 
     "noImplicitAny": false, 
     "module": "system", 
     "moduleResolution": "node", 
     "sourceMap": false, 
     "emitDecoratorMetadata": true, 
     "experimentalDecorators": true, 
     "removeComments": true, 
     "outDir":"js/" 
    }, 
    "exclude": [ 
    "bower_components", 
    "node_modules", 
    "wwwroot" 
    ] 
} 
+0

什麼tsconfig.json看起來不一樣?你在用commonjs嗎?如果AMD/es6需要導入模塊。你指定和「出」? 例如https://github.com/ca0v/ags-lab/blob/master/tsconfig.json –

+0

@CoreyAlix我剛剛添加了tsconfig的問題。 – BeniaminoBaggins

+0

我認爲「outDir」不正確。你想把它全部放在一個文件中?試用」。否則,你需要導入。 –

回答

0

嘗試

  1. 取出「輸出模塊區{」
  2. 進口面積=要求(「./款/ area_getter」)

,消除了明確的模塊名稱,並讓您的進口中的名字。

所以F1:

export interface iGetAreas { 
} 

export abstract class AreaGetter implements iGetAreas { 
} 

export class MultipleSelectAreaGetter extends AreaGetter { 
} 

export class SingleSelectAreaGetter extends AreaGetter { 
} 

F2:

import Area = require("./f1"); 

class SearchFilterViewModel { 
    _regionGetter: Area.SingleSelectAreaGetter; 
    _townGetter: Area.SingleSelectAreaGetter; 
    _suburbGetter: Area.MultipleSelectAreaGetter; 
} 
+0

你的意思是我應該嘗試在任何地方都沒有模塊?這只是一個類的導入? – BeniaminoBaggins

+0

不是一個類,只是一個匿名模塊。也許你應該輸入「ko」呢? import ko = require(「./ path/to/knockout」); 導出接口iGetAreas {0} _areasList:Array ; _areas:KnockoutObservableArray ; getAreas(geonameId:string):void; } –

+0

我收到這些錯誤:> system-csp-production.src.js:3217 Uncaught TypeError:多個匿名System.register在同一個模塊中調用file.t @ system-csp-production.src.js:3217l .register @ system-csp-production.src.js:3217(匿名函數)@ search_filter_view_model.js:1 bootstrap_app。js:2 Uncaught ReferenceError:SearchFilterViewModel未定義 我的導入語句沒有紅色的縮進。我使用'import Area = require(「../ models/area_getter.ts」);'你知道我爲什麼有這些錯誤嗎? – BeniaminoBaggins