进口服务时,我应该在“进口"中进口它们吗?NgModule 中的数组或提供者数组? [英] When import services, should I import them in "imports" array or providers array in NgModule?

查看:25
本文介绍了进口服务时,我应该在“进口"中进口它们吗?NgModule 中的数组或提供者数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Angular Doc,NgModule 中关于 imports 的定义是:

From Angular Doc, the def about imports in NgModule is:

指定模块的列表,其导出的指令/管道应可用于此模块中的模板.

Specifies a list of modules whose exported directives/pipes should be available to templates in this module.

提供者:

定义在此模块的注入器中可用的可注入对象集.

Defines the set of injectable objects that are available in the injector of this module.

所以这里的问题是,当我想使用打包在第三个模块中的第三个服务时,我应该在 imports 中导入该模块,还是在 providers<中注册特定的服务/代码>?

So here is the question, when I want to use a 3rd service packed in a 3rd module, should I import the module in imports, or register the particular service, in providers?

推荐答案

一般来说,添加到任何模块的 providers 数组的所有服务都是应用程序范围的单例 - 这意味着它们会自动添加到根注入器中.无需将它们显式添加到 AppModule 的 providers 数组中.

In general, all services added to the providers array of any module are application-wide singletons - meaning they are added to the root injector automatically. There is no need to explicitly add them to the providers array of your AppModule.

这意味着当你导入一个包含了 providers 数组的模块(或另一个模块导入了一个模块)时,它们提供的服务可以被注入到任何可注入的构造函数中——不管它在哪里.只需导入服务,并将其添加到可注入的构造函数中.

That means that when you import a module (or another module imports a module) that has the providers array populated, the services they provide can be injected into any injectable constructor - regardless of where it is. Just import the service, and add it to an injectable constructor.

现在,我说一般",因为在大多数情况下这是真的.唯一不同的情况是模块是延迟加载的模块(即加载延迟加载的路由).延迟加载的模块有自己的根作用域.如果您将其视为例外,则一切都有意义.

Now, I say "in general" because for the most part that's true. The only case where it is different is when the module is a lazy loaded module (i.e. loading lazy-loaded routes). Lazy-loaded modules have their own root scope. If you treat that as the exception, it all makes sense.

所以回答你的问题:

  1. 首选 module.forRoot() 如果它有这样的方法.这将返回一个带有服务的模块,它将被根注入器引导.

  1. Prefer module.forRoot() if it has such a method. This returns a module with services, which will get bootstrapped with the root injector.

@NgModule({
    // forRoot ensures that services are added to root injector
    imports: [ThirdPartyModule.forRoot()],
    exports: [],
    declarations: [...],
    providers: [],
})
export class AppModule{ }

  • 如果没有forRoot"方法,那么只需将模块导入AppModule即可.很可能,该模块具有旨在向根注入器注册的服务.

  • If there is no "forRoot" method, then just import the module into AppModule. Likely, the module has services intended to be registered with the root injector.

    @NgModule({
        imports: [ThirdPartyModule],
        exports: [],
        declarations: [...],
        providers: [],
    })
    export class AppModule{ }
    

  • 如果您需要从 FeatureModule(或 SharedModule)导入模块,因为您想利用其中包含的组件、指令和管道,您应该使用 forChild() 方法(如果有).

    @NgModule({
        // does not include any services. You still need to import
        // .forRoot() in the AppModule
        imports: [ThirdPartyModule.forChild()],
        exports: [],
        declarations: [...],
        providers: [],
    })
    export class FeatureModule { }
    

  • 如果模块的作者没有提供 forRoot()forChild() 方法,它可能从未打算被导入AppModule 以外的任何模块.

  • If the author of the module has not provided a forRoot() or forChild() method, it may be it was never intended to be imported by any module other than the AppModule.

    模块的作者可能已经决定在模块中包含服务,但默认情况下选择不将其添加到提供者数组中.在这种情况下,您可以将其添加到模块的 providers 数组中(服务将具有根范围),或者将其添加到组件的 providers 数组中(服务将具有组件范围).

    The author of the module may have decided to include services in the module, but by default chose not to add it to the providers array. In that case its up to you to add it to a providers array of a module (services will have root scope), or add it to a component's providers array (services will have component scope).

    这篇关于进口服务时,我应该在“进口"中进口它们吗?NgModule 中的数组或提供者数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆