繁体   English   中英

无法使用Typescript导入模块

[英]Can't import modules with Typescript

我很难导入模块A:

export module A {
    export class A_Class {
    }
}

进入我的模块B:

import { A } from "./a";

let a = new A.A_Class();

我的tsconfig.json看起来像这样:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true
  },
  "compileOnSave": true,
  "files": [
    "a.ts",
    "b.ts",
  ]
}

我的cshtml具有以下脚本部分:

<script type="module" src="~/Scripts/app/b.js"></script>

chrome浏览器给我错误:

http://localhost:64518/Scripts/app/a net::ERR_ABORTED 404 (Not Found)

对于b.js line 1 b.js

import { A } from "./a";

我完全没有主意,尝试了es6commonjs等的许多组合,而import错误略有不同。

我正在使用Typescript 3.0

尝试更改此:

import { A } from "./a";

对此:

import { A } from "./a.js";

文件“ a”实际上不存在,因此Chrome无法找到它。 虽然a.js应该存在。

如果您是在服务器后面运行此程序,或者正在使用诸如SystemJS之类的模块加载器,则可能会导致没有文件扩展名的请求在找不到文件的情况下将.js添加到请求中,否则您将需要在导入中手动添加扩展名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM