无法在Typescript中导出常量 [英] Can't export constant in Typescript

查看:1067
本文介绍了无法在Typescript中导出常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我吗

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

I have 2 files main.ts and hi.ts

hi .ts:

export const hello = "dd";

main.ts:

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

我有例外:


未捕获的ReferenceError:未定义hello(...)

Uncaught ReferenceError: hello is not defined(…)

如何从A类中看到这个const变量?有可能吗?

How can I see this const variable from class A? Is it possible?

推荐答案

我的答案是指Typescript 2 +。

My answer refers to Typescript 2+.

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

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

唯一的区别是你的代码& mine是import语句中的*运算符。

The only difference b/w your code & mine is the * operator in the import statement.

这篇关于无法在Typescript中导出常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆