将类模块导入另一个类

时间:2016-10-27 11:36:24

标签: typescript webstorm

我正在尝试将一些类导入到我的主类中,但是会出错;未捕获的SyntaxError:行导入时意外的令牌导入ApiDto = Model.DtoModel.ApiDto;

enter image description here

两个DTO模型在两个文件中共享相同的模块名称;

export module DtoModel { 
    export class ApiDto {
    }
    export class ApiDtoItem { 
        public method: string;
        public object: any;
    }
}

export module DtoModel { 
    export class User { 
        public userNo: number;
    }
}

这是项目结构;

enter image description here

Main.ts

/// <reference path="jquery.d.ts" />

import * as Model from "../DtoModels/./ApiDto";

// breaks on next line!
import ApiDto = Model.DtoModel.ApiDto;

export class Main {
    private Url: string;

    constructor() {
        var apidto = new ApiDto();
        this.Url = "http://localhost:80/api/main";

        var response = jQuery.post(this.Url, (data: ApiDto, textStatus: string, jqXHR: JQueryXHR) => {
           apidto = data;
        });

        console.log(response);
    }
}

var main: Main = new Main();

以下是我的编译选项:

{
  "compilerOptions": {
    "target": "es6",
    "noImplicitAny": true,
    "noEmitOnError": false,
    "sourceMap": true,
    "inlineSources": true
  },
  "compileOnSave": true
}

1 个答案:

答案 0 :(得分:1)

您正在将您的打字稿编译为ES6(tsconfig.json中为"target": "es6")。但是浏览器本身并不支持ES6导入/导出 - 因此错误。