【问题标题】:export/import module not working typescript导出/导入模块不起作用打字稿
【发布时间】:2016-07-13 19:14:15
【问题描述】:

我在文件夹 models/area_getter.ts 中有一个名为 Area 的模块:

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 识别区域模块?

我会做类似import { Area } from "./models/area_getter"; 的事情吗?

这在import语句中有一个错误说

找不到模块区域

这是我的文件夹结构:

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"
  ]  
}

【问题讨论】:

  • tsconfig.json 是什么样的?你用的是commonjs吗?如果是 AMD/es6,则需要导入模块。你指定和“出”吗?例如。 github.com/ca0v/ags-lab/blob/master/tsconfig.json
  • @CoreyAlix 我刚刚将 tsconfig 添加到问题中。
  • 我认为“outDir”不正确。你想把它全部放在一个文件里吗?试用”。否则需要导入。
  • @CoreyAlix 我不认为我希望将 js 放在一个文件中,因为在调试时找到特定的代码行会有点困难,不是吗?
  • 那你需要导入。请参阅下面答案中的 f1 和 f2 示例。

标签: javascript import module typescript export


【解决方案1】:

试试

  1. 删除“导出模块区域{”
  2. import Area = require("./models/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;
}

【讨论】:

  • 你的意思是我应该尝试在任何地方都没有模块吗?那个导入只是一个类吗?
  • 不是一个类,只是一个匿名模块。也许你也应该导入“ko”? import ko = require("./path/to/knockout");导出接口 iGetAreas { _areasList: Array; _areas: KnockoutObservableArray; getAreas(geonameId:字符串):无效; }
  • 我收到这些错误:> system-csp-production.src.js:3217 Uncaught TypeError: Multiple anonymous System.register calls in the same module file.t @ system-csp-production。 src.js:3217l.register @ system-csp-production.src.js:3217(anonymous function) @ search_filter_view_model.js:1 bootstrap_app.js:2 Uncaught ReferenceError: SearchFilterViewModel is not defined 我的导入语句没有红色波浪线。我用import Area = require("../models/area_getter.ts");你知道我为什么会出现错误吗?
  • 不,我使用 requirejs 和 AMD,所以我不确定,但尝试从导入中删除 .ts 扩展名。只是“../models/area_getter”。
猜你喜欢
  • 1970-01-01
  • 2019-08-29
  • 2017-05-07
  • 2016-11-21
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 2017-10-29
  • 2017-12-10
相关资源
最近更新 更多