簡體   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