如果你想要在一个typescript项目中包含多个文件,可以使用以下方法:
// fileA.ts
namespace MyNamespace {
export function doSomething() {
console.log("Doing something...");
}
}
// fileB.ts
///
namespace MyNamespace {
export function doAnotherThing() {
doSomething(); // 调用fileA.ts中的函数
console.log("Doing another thing...");
}
}
// main.ts
///
MyNamespace.doAnotherThing(); // 调用fileB.ts中的函数
import
和export
语句来访问其他文件中的代码。例如:// fileA.ts
export function doSomething() {
console.log("Doing something...");
}
// fileB.ts
import { doSomething } from './fileA';
export function doAnotherThing() {
doSomething(); // 调用fileA.ts中的函数
console.log("Doing another thing...");
}
// main.ts
import { doAnotherThing } from './fileB';
doAnotherThing(); // 调用fileB.ts中的函数
无论你选择哪种方法,都需要确保在编译或打包时,所有文件被正确地包含和链接在一起。
下一篇:包括多个未定义的变量