[英]Imports with typescript
I begin a new project with typescript and I have questions about imports.我用打字稿开始了一个新项目,但我对导入有疑问。 Let's assume I have these 2 files:
假设我有这两个文件:
// foo.ts
namespace root {
export class Test {
constructor() {
console.log(BAR)
}
}
}
// a/bar.ts
namespace root.a {
export const BAR = 'Whatever'
}
How can I use my BAR const into my Test class?如何在我的测试类中使用我的 BAR 常量? I tried to add
/// <reference path="a/bar.ts" />
and then I can call root.a.BAR
or even a.BAR
.我尝试添加
/// <reference path="a/bar.ts" />
然后我可以调用root.a.BAR
甚至a.BAR
。
But I would prefer to not use this reference statement, is it possible?但我宁愿不使用这个参考声明,这可能吗? Also, I would like to use
BAR
without its full namespace, is it also possible?另外,我想在没有完整命名空间的情况下使用
BAR
,这也可能吗?
Thank you in advance,提前谢谢你,
Xavier泽维尔
I would prefer to not use this reference statement, is it possible
我宁愿不使用此参考声明,是否可能
Yes, you would use tsconfig.json : https://basarat.gitbook.io/typescript/project/compilation-context/tsconfig是的,你会使用 tsconfig.json : https ://basarat.gitbook.io/typescript/project/compilation-context/tsconfig
but beware of ordering pain (you will need to order using files
) : https://basarat.gitbook.io/typescript/content/docs/tips/outFile.html但要注意订购痛苦(您需要使用
files
订购): https : //basarat.gitbook.io/typescript/content/docs/tips/outFile.html
Instead use modules.而是使用模块。
https://basarat.gitbook.io/typescript/content/docs/project/modules.html https://basarat.gitbook.io/typescript/content/docs/project/modules.html
Thank you for your answer, basarat.谢谢你的回答,basarat。
I use namespaces because I don't want to set up something like RequireJS, required by modules .我使用命名空间是因为我不想设置像 RequireJS 这样的东西,这是modules 所要求的。 But if I understand well, it is not the right way to solve my problem.
但如果我理解得很好,这不是解决我的问题的正确方法。
I tried to follow this example (typescript + angular 1): hottowel-angular-typescript by John Papa but is it really a good example?我试图遵循这个例子 ( typescript + angular 1): hottowel-angular-typescript by John Papa但这真的是一个很好的例子吗? I mean, I really don't want to prefix every class / interface / const ... by its namespace!
我的意思是,我真的不想在每个类/接口/常量 ... 前面加上它的命名空间!
Is there another solution without file modules?有没有没有文件模块的另一种解决方案?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.