Typescript-如何在node.js上声明全局变量

问题描述 投票:1回答:1

如何全局声明一些自定义变量?

用法示例

import './logger'; // custom module: Global logger is initialized. (using winston)

log.info('This is custom global logger.'); // access to log variable globally.
node.js typescript typescript-typings
1个回答
0
投票

在TypeScript中,声明全局块用于描述向全局名称空间添加变量或声明全局变量。

Node.js

import * as io from 'socket.io';

declare global {
  namespace NodeJS {
    interface Global {
      SocketServer: io.Server
    }
  }
}

global.SocketServer = io.default();

注:在某些情况下,即使使用多个进程,全局变量也可以。当我们想要存储恒定值时(例如在出现故障时的一些电子邮件ID),建议使用/常用。 global.support_email =“ [email protected]

参考文献可能重复:

How to use global variable in node.js?

How to create writable global variable in node.js typescript(使用打字稿)

How to use global variable in node.js?

© www.soinside.com 2019 - 2024. All rights reserved.