首页 > 解决方案 > 使用模块级 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

标签: javascripttypescript

解决方案


它只是一个模块级常量:)

class Interal {
  // Stuff here
}

const instance = new Internal()

export default instance

推荐阅读