javascript - 使用模块级 const 创建单例 - 打字稿
问题描述
我在 Typescript 中创建了 Singleton 类,如下所示 -
export class Service {
public static readonly I = new Service();
public post(url: string): string {}
}
来电者使用它喜欢Service.I.post('url');
。
我得到了使用建议module-level const
。它是什么?如何使用创建单例类型类Module-level const
?
解决方案
它只是一个模块级常量:)
class Interal {
// Stuff here
}
const instance = new Internal()
export default instance
推荐阅读
- vue.js - Gridsome Graphql 过滤器变量
- perl - 使用 perl 解析特定的 JSON
- jenkins - Sonarqube - java.lang.OutOfMemoryError:超出 GC 开销限制
- kubernetes-helm - 带有 alb 入口控制器的 Graylog helm
- python - Chrome 无头模式卡在 ws 上听
- reactjs - 在 Mapbox GL JS 弹出窗口中使用链接会引发错误:不变式失败:您不应该在外部使用
- python - Python 不理解 PYTHONPATH 中的 PYX 文件
- python - Jinja2 通过 cx_freeze 到 exe 不起作用。pkg_resources 问题
- asp.net-core - ASP.NET Core:JavaScript 文件被缓存
- c++ - 方法函数与 C++ 中的库函数同名