Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在Typescript中导出常量_Typescript_Import_Export_Constants - Fatal编程技术网

Can';t在Typescript中导出常量

Can';t在Typescript中导出常量,typescript,import,export,constants,Typescript,Import,Export,Constants,有人能帮我吗 我有两个文件main.ts和hi.ts 嗨,ts: export const hello = "dd"; main.ts: import { hello } from "./hi"; ... class A { public sayHello() { console.log("hello=" + hello); } ... } 我有例外: 未捕获引用错误:未定义hello(…) 如何从类A中查看此常量变量?可能吗?我的答案是关于TypeS

有人能帮我吗

我有两个文件main.ts和hi.ts

嗨,ts:

export const hello = "dd";
main.ts:

import { hello } from "./hi";
...
class A {
    public sayHello() {
        console.log("hello=" + hello);
    }
    ...
}
我有例外:

未捕获引用错误:未定义hello(…)


如何从类A中查看此常量变量?可能吗?

我的答案是关于TypeScript 2+

// 1.ts
export const AdminUser = { ... }

// index.ts
import * as users from './docs/users/admin';
var adminUser = users.AdminUser;

您的代码和我的代码之间的唯一区别是import语句中的
*
运算符。

请参见此问题:我确实导出const hello=“dd”;就像在你的链接中一样,但它在Node.js或浏览器中不工作?在浏览器中(chrome和firefox相同)