gpt4 book ai didi

javascript - 将类放入单独的文件中时出现 Typescript 错误

转载 作者:行者123 更新时间:2023-11-27 23:40:30 28 4
gpt4 key购买 nike

我觉得这是我犯的一个非常基本的错误,但我在任何地方都找不到解决方案。我有两个类(class),Moo.ts:

module namespace {
export class Moo{
constructor() {
// window.console.log("hello from Moo");
var foo:Foo = new Foo();
}
}

和Foo.ts:

 module namespace {
export class Foo{
constructor() {
// window.console.log("hello from Foo");
}
}

运行 Moo.ts 时,我希望它创建 Foo 的实例,但事实并非如此。它给出了这个错误:

TypeError: namespace.Foo is not a constructor

当我将两个类放在同一个 ts 文件中时,它工作得很好,但是当我将它们分解时,一切都会崩溃。我错过了什么吗?

最佳答案

您可能在 html 中的 Moo.js 之后包含了 Foo.js(生成的文件),这意味着当 Moo.js > run(因为它是第一个js文件)它没有找到namespace.Foo

包含生成的 js 文件的顺序很重要。这就是为什么你最好使用 tsconfig.json并在您的 html 中包含一个生成的 js 文件(您可以使用源映射来调试 TypeScript 代码)

关于javascript - 将类放入单独的文件中时出现 Typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713734/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com