我尝试在单个命名空间但在多个文件中编写 typescript 代码。但是当我在 AAA
类中使用类 config
时,Config
没有定义,我有语法错误。为什么?
检查这个例子:
文件:services/Config/index.ts
:
export namespace Services {
export class Config {
/* class methods */
}
}
文件:services/AAA/index.ts
:
/// <reference path = "./../Config/index.ts" />
export namespace Service {
export class AAAA {
private config = new Config(); <-- Error is here
/* class methods */
}
}
最佳答案
您不导出命名空间:
namespace Services {
export class Config {
/* class methods */
}
}
/// <reference path = "../Config/index.ts"/>
namespace Service {
export class AAAA {
private config = new Config(); <-- Error is here
/* class methods */
}
}
https://www.typescriptlang.org/docs/handbook/namespaces.html
关于跨多文件的 Typescript 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521789/