共享模块

Nest Modules can export their components. It means that we can easily share component instance between them. The best way to share an instance between two or more modules is to create Shared Module.

Nest模块可以导出其组件。也就是说我们可以轻松在模块之间共享组件。 模块间共享组件的最好方式是创建共享模块图片标题

For example - if we want to share ChatGateway component across entire application, we could do it in this way:

例如---如果我们想在整个应用程序中共享ChatGateway组件,我们可以按照如下方式进行:

import { Module, Shared } from '@nestjs/common';

@Shared()
@Module({
    components: [ ChatGateway ],
    exports: [ ChatGateway ]
})
export class SharedModule {}

Then, we only have to import this module into another modules, which should share component instance:

接下来,我们只需要将这个要分享组件实例的模块导入到其他模块中即可:

@Module({
    modules: [ SharedModule ]
})
export class FeatureModule {}

第三方模块

If you want to share 3rd-party module, you can use Shared as a function:

如果你想共享第三方模块,你可以将Shared看作一个函数来使用:

@Module({
    modules: [ Shared()(ThirdPartyModule) ]
})
export class FeatureModule {}

范围

If you don't want to share components across entire application, but only within smaller, defined scope, you can use a namespace-scoped @Shared('namespace') module.

如果你不想在整个应用程序中共享组件,只想在比较小的指定的范围内分享组件的话,你可以使用namespace-scoped*``@Shared('namespace')模块。

@Module({
    modules: [ Shared('namespace')(ThirdPartyModule) ]
})
export class FeatureModule {}

results matching ""

    No results matching ""