框架依赖注入和普通依赖注入

    科技2023-11-20  110

    框架依赖注入和普通依赖注入

    Angular’s Dependency Injection System is a magic way to deliver the classes that house your business rules, or encapsulate other functionality, to your components. Here are three tips for using the dependency injector that are good to keep in mind as you learn, use, and master Angular.

    Angular的依赖注入系统是一种将包含业务规则或封装其他功能的类传递到组件的神奇方法。 这里是使用依赖注入器的三个技巧,在学习,使用和掌握Angular时要牢记这些技巧。

    1.首选ProvidedIn (1. Prefer ProvidedIn)

    When you create services, by default, use the ProvidedIn setting to put those services into the root injector. This gives you the safest, easiest and simplest way to create services. You won’t forget to add them to your module, and this method works best for 99.9% of all use cases. It looks like this:

    创建服务时,默认情况下,使用ProvidedIn设置将这些服务放入根注入器。 这为您提供最安全,最简单的方法来创建服务。 您将不会忘记将它们添加到模块中,并且此方法最适合所有用例的99.9%。 看起来像这样:

    The string ‘root’ has to be exactly that. That registers your service, and lets you just keep coding, no need to open up your module file and add it there. Easy Peasy.

    字符串“ root”必须正是该字符串。 这将注册您的服务,并让您仅进行编码即可,而无需打开模块文件并将其添加到此处。 十分简单。

    2.学习useValue (2. Learn useValue)

    We default to only using services that are “classes” meaning full-on, constructed classes with properties, methods, etc. But what if you have an object you got from somewhere that you’d like to use, such as from a 3rd party library, or a JSON file, or what if you want to inject just a single value, like a timeout value.

    我们默认只使用“类”的服务,即包含属性,方法等的完整类,构造类。但是,如果您有一个对象是从您想使用的地方(例如从第三方)获得的,该怎么办?库或JSON文件,或者如果您只想注入一个值(如超时值),该怎么办。

    Or maybe a collection of values

    或者也许是价值的集合

    There’s no functionality here, so a class can be overkill. And if you get these values from somewhere else, you would need to then wrap them in a class.

    这里没有任何功能,因此一堂课可能会显得过大。 而且,如果您从其他地方获得这些值,则需要将它们包装在一个类中。

    To make this easier, you can just take single values, or simple objects, and create services out of them if you learn the useValue provider. You can find documentation on that here.

    为了使此操作更容易,如果您学习了useValue提供程序,则可以只采用单个值或简单对象,并从中创建服务。 您可以在此处找到有关此文档的文档。

    3.无根注入是一种代码气味 (3. Non-Root Injection is a Code Smell)

    When creating services, registering them in the root injector is the default, which you can do using either the ProvidedIn syntax shown above, or by registering it in the providers section of your app.module file.

    创建服务时,默认情况是在根注入器中注册它们,您可以使用上面显示的ProvidedIn语法或在app.module文件的providers部分中注册它来完成。

    There are plenty of reasons to register a service somewhere else besides your root injector, and instead provide them in a component, a directive, or in a pipe. But whenever you use one of these methods, you should DEFINITELY understand what you’re doing, and the ramifications of that decision. So always consider this method a code smell. If you are considering using it, double-check that you are making a good decision, and actually need to do that.

    有很多原因可以在根注入器之外的其他地方注册服务,而是在组件,指令或管道中提供服务。 但是,无论何时使用这些方法之一,您都应该明确了解您在做什么以及该决定的后果。 因此,请始终将此方法视为代码气味。 如果您正在考虑使用它,请仔细检查您是否做出了正确的决定,并且确实需要这样做。

    And there you have it: three tips for Angular’s dependency injection.

    到此为止:Angular依赖项注入的三个技巧。

    Happy coding!

    编码愉快!

    Signup for my newsletter here.

    在这里注册我的新闻通讯。

    Visit Us: thinkster.io | Facebook: @gothinkster | Twitter: @GoThinkster

    访问我们: thinkster.io | 脸书: @gothinkster | 推特: @GoThinkster

    翻译自: https://medium.com/thinkster-io/3-angular-dependency-injection-tips-c4b5356541ee

    框架依赖注入和普通依赖注入

    Processed: 0.010, SQL: 8