无法在 Typescript 中导出常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39385933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't export constant in Typescript
提问by EgorTitov
Can someone help me please
有人能帮助我吗
I have 2 files main.ts and hi.ts
我有 2 个文件 main.ts 和 hi.ts
hi.ts:
hi.ts:
export const hello = "dd";
main.ts:
main.ts:
import { hello } from "./hi";
...
class A {
public sayHello() {
console.log("hello=" + hello);
}
...
}
I have exception:
我有例外:
Uncaught ReferenceError: hello is not defined(…)
未捕获的 ReferenceError: hello is not defined(...)
How can I see this const variable from class A? Is it possible?
如何从 A 类中看到这个 const 变量?是否可以?
回答by Socratees Samipillai
My answer refers to Typescript 2+.
我的回答是指 Typescript 2+。
// 1.ts
export const AdminUser = { ... }
// index.ts
import * as users from './docs/users/admin';
var adminUser = users.AdminUser;
The only difference b/w your code & mine is the * operator in the import statement.
您的代码和我的代码的唯一区别是导入语句中的 * 运算符。