基本上我想做的是将每个类放在单独的 .ts 文件中
测试.ts
export class TestClass{
constructor(){}
public color: string = 'red';
}
test2.ts
/// <reference path="test.ts"/>
var classt = new TestClass();
alert(classt.color);
当我尝试编译它时,它给出错误:“test2.ts(8,18): error TS2304: Cannot find name 'TestClass'.”。可以这样吗?
最佳答案
/// <reference path="test.ts"/>
不是提取其他文件的建议方法。
建议的方法是导入文件:
import { TestClass } from './test.ts';
// use TestClass
话虽这么说,要修复您所拥有的内容,您仍然可以使用三斜杠引用,但您需要它是本地路径...
/// <reference path="./test.ts"/>
// note the ./ in the reference above
关于class - 从另一个 *.ts 文件创建 typescript 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32914487/