This article is a mirror article of machine translation, please click here to jump to the original article.

View: 1359|Reply: 1

[Angular] Angular 18 Series (Twenty-Eight) InjectionToken Brief Introduction

[Copy link]
Posted on 2024-12-20 16:02:20 | | | |
Angular is a popular front-end JavaScript framework that provides a powerful way to build single-page applications (SPAs). In Angular, Dependency Injection (DI) is a key feature that allows us to efficiently manage dependencies in our applications. Angular's dependency injection system uses InjectionTokens to implement certain special dependency injection requirements. In this article, I will explain in detail what InjectionToken does and provide examples to illustrate its practical use in Angular applications.

What is dependency injection?

Before diving into InjectionToken, let's first understand what dependency injection is. Dependency injection is a design pattern that allows us to inject dependencies of one object (e.g., services or configurations) into another object without hardcoding those dependencies. The benefits of this include:

1. Maintainability: By injecting dependencies into components, we can easily change these dependencies without having to modify extensive code.
2. Testability: We can easily provide simulated dependencies for components to unit test without actually creating instances of those dependencies.
3. Loose Coupling: Dependency injection helps us achieve loose coupling, making the relationships between individual components more flexible.

In Angular, dependency injection is built-in, and Angular's dependency injection container is responsible for managing the creation and lifecycle of dependencies.

Why do you need an InjectionToken?

Sometimes we need to inject dependenciesNot an instance of a class, but a configuration item, string, or other non-class value。 This is where InjectionToken comes in, allowing us to inject non-class values into components or services as dependencies.

What does InjectionToken do?

The role of an InjectionToken is to define an identifier that identifies dependencies. It allows us to inject any value into an Angular component or service, not just an instance of a class. Normally, we create an InjectionToken somewhere in the application and use it where we need to inject that value.

Here are the main functions of InjectionToken:

1. Unique Identification: The InjectionToken is a unique identifier that ensures the uniqueness of dependencies. This is important to prevent confusion or conflicts in dependencies.
2. Non-Class Dependency Injection: InjectionToken allows us to inject any value, not just an instance of a class. This is useful in scenarios such as configurations, constants, strings, etc.
3. Provider configuration: Through provider configuration, we can tell Angular how to provide an instance of the dependency for the InjectionToken. This allows us to provide different values for InjectionTokens in different contexts.

Now, let's detail the usage and role of InjectionToken with some examples.

Inject the app configuration

For example, we inject an application configuration with the URL address and environment name, and the code is as follows:


From the constructor injection, the printout is as follows:



Reference:

The hyperlink login is visible.
The hyperlink login is visible.




Previous:.NET/C# console services are registered with Consul service discovery
Next:Call the Alibaba Cloud OpenAPI API to get server usage
 Landlord| Posted on 2025-2-17 13:36:08 |
InjectionToken and BehaviorSubject combine to build a global dynamic value




Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com