无法使用Typescript导入模块

问题描述 投票:0回答:1

我很难尝试导入模块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)

对于line 1上的b.js

import { A } from "./a";

我完全没有想法,并尝试过es6commonjs等的许多组合,import的错误只是变化了一点点。

我正在使用Typescript 3.0

typescript razor visual-studio-2017
1个回答
1
投票

尝试改变这个:

import { A } from "./a";

对此:

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

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

如果你在服务器后面运行它,或者如果你正在使用像SystemJS这样的模块加载器,你可能能够在没有文件扩展名的情况下向请求添加.js,如果找不到文件,否则你需要在您的导入中手动输入扩展名。

© www.soinside.com 2019 - 2024. All rights reserved.