如何在 Typescript 中导出类实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54351492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:46:26  来源:igfitidea点击:

How to export a class instance in Typescript

angulartypescriptumd

提问by mindparse

Im authoring a TS library, and would like to export an instance of a class, I intend for it be used as singleton in the consuming application.

我正在创作一个 TS 库,并想导出一个类的实例,我打算将它用作消费应用程序中的单例。

Right now I have the following structure:

现在我有以下结构:

index.ts

索引.ts

export { Foo } from './my-class';

foo.ts

export class Foo {
  functionA() {}
}

I'm then building into UMD format using webpack and babel, and in another application (Angular), I am able to import in my class, instantiate it and use it accordingly.

然后我使用 webpack 和 babel 构建成 UMD 格式,并且在另一个应用程序(Angular)中,我能够在我的类中导入,实例化它并相应地使用它。

import { Foo } from 'foo';

private foo = new Foo();

const x = foo.functionA();

Is there a way to return an instantiated instance of my class or am I thinking about this the wrong way?

有没有办法返回我的类的实例化实例,或者我是否以错误的方式思考这个问题?

So instead of having to do new Foo(), the imported Foo would actually already be an instance?

因此,不必做new Foo(),导入的 Foo 实际上已经是一个实例?

Thanks

谢谢

UPDATEI should have mentioned, I am exporting other things such as interfaces, so I don't think a default class export would be the right way to go correct? - see here

更新我应该提到,我正在导出其他东西,比如接口,所以我认为默认的类导出不是正确的方法吗?- 看这里

回答by

You can control what you're returning like so:

您可以像这样控制返回的内容:

// Export the named class directly
export class Foo { }

// Export the named class indirectly
class Bar { }
export { Bar }

// Export an instance of the class directly
export const foo = new Foo();

// Export an instance of the class indirectly
const bar = new Bar();
export { bar };

Here's a TypeScript Playground linkshowing the code compiles and the produced javascript.

这是一个TypeScript Playground 链接,显示了代码编译和生成的 javascript。

The TypeScript Handbook official documentation for exports (and imports, and re-exports): https://www.typescriptlang.org/docs/handbook/modules.html#export

用于导出(和导入和重新导出)的 TypeScript 手册官方文档:https: //www.typescriptlang.org/docs/handbook/modules.html#export

The MDN documentation (courtesy of jo_va): https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

MDN 文档(由 jo_va 提供):https: //developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

And here's Basarat's guide for them: https://basarat.gitbooks.io/typescript/docs/project/modules.html

这是他们的 Basarat 指南:https://basarat.gitbooks.io/typescript/docs/project/modules.html

回答by jo_va

Yes this is totally possible, and this is a standard way to do it in TS too.

是的,这是完全可能的,这也是在 TS 中执行此操作的标准方法。

export default new Foo();

If however you want to import not only this instance but interfaces as well, you would export the singleton like this:

但是,如果您不仅要导入此实例,还要导入接口,则可以像这样导出单例:

export const foo = new Foo();

You can find the exportdoc here

你可以在这里找到export文档

回答by ABOS

If you want singleton only, you should stick to the singleton convention,

如果你只想要单例,你应该坚持单例约定,

  export class Foo {
    private static _instance = new Foo();
    private constructor() {

    }

    static get instance() {
      return this._instance;
    }

  }

  export const foo = Foo.instance;

回答by amul klinton

It is possible to export the instance of the class Members.

可以导出类成员的实例。

Export the class instance like this: export const playerRoutes = new Routes

像这样导出类实例: export const playerRoutes = new Routes

Export the class like this: export class player

像这样导出类: export class player