为了账号安全,请及时绑定邮箱和手机立即绑定

类型脚本导入在编译为 java 脚本时缺少.js扩展名

类型脚本导入在编译为 java 脚本时缺少.js扩展名

蝴蝶刀刀 2022-09-16 20:50:49
我决定将我的js项目迁移到ts中,但是我面临着以下问题:ts文件中的所有导入都缺少编译的js文件中的.js扩展名。这反过来又会引发以下错误:在我的浏览器控制台上。这是代码Loading failed for the module with source “http://localhost:5500/build/test/first”./src/index.html<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body></body><script src="/build/test/last.js" type="module"></script></html>/src/test/first.tsexport class First {    name: string;    constructor(name: string) {        this.name = name    }}/src/test/last.tsimport {First} from "./first"class Last {    constructor() {        let name = new First("this is my name").name;        console.log(name)    }}new Last();/构建/测试/第一.jsexport class First {    constructor(name) {        this.name = name;    }}/构建/测试/最后.jsimport { First } from "./first";class Last {    constructor() {        let name = new First("this is my name").name;        console.log(name);    }}请注意,在最后.js,导入缺少.js扩展名,如果我手动添加缺少的扩展名,则所有内容都按预期工作。最后是我的ts配置{    "compilerOptions": {        "target": "ESNext",        "lib": ["DOM","ES2017", "DOM.Iterable", "ScriptHost"],        "watch": true,        "rootDir": "./src",         "outDir": "./build",        "sourceMap": true,        "removeComments": true,        "noEmitOnError": true,        "strict": true,    }}我是否缺少一些未在导入上添加正确扩展名的内容?如果是这样,请告诉我我做错了什么。谢谢。
查看完整描述

1 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

我是否缺少一些未在导入上添加正确扩展名的内容?

为什么类型脚本会随机更改您的导入?您告诉它导入一个名为 的模块,并正确地将其编译为名为 的模块的导入。./first./first

如果要导入其他模块,则需要告诉 TypeScript 导入其他模块。

因此,如果您不想导入名为 的模块,而是要导入名为 的模块,则需要告诉 TypeScript 导入一个名为 的模块,而不是一个名为 的模块:./first./first.js./first.js./first

import {First} from "./first.js"


查看完整回答
反对 回复 2022-09-16
  • 1 回答
  • 0 关注
  • 88 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信