Home >Web Front-end >JS Tutorial >Take you to understand component communication and dependency injection in Angular
AngularHow to communicate between components? What is dependency injection? The following article will give you a brief understanding of the component communication method and introduce dependency injection. I hope it will be helpful to you!
##1.1 Passing data to the inside of the component
<app-favorite [isFavorite]="true"></app-favorite>
// favorite.component.ts import { Input } from '@angular/core'; export class FavoriteComponent { @Input() isFavorite: boolean = false; }
Note: Add[]
outside the attribute to indicate binding the dynamic value. After receiving it in the component, it will be a Boolean type. Do not add
[]to indicate Bind a normal value, which is a string type after being received in the component. [Related tutorial recommendations: "
angular tutorial"]
1.2 Components transmit data to the outside
Requirements : Pass data to the parent component by clicking the button in the child component<!-- 子组件模板 --> <button (click)="onClick()">click</button>
// 子组件类 import { EventEmitter, Output } from "@angular/core" export class FavoriteComponent { @Output() change = new EventEmitter() onClick() { this.change.emit({ name: "张三" }) } }
<!-- 父组件模板 --> <app-favorite (change)="onChange($event)"></app-favorite>
// 父组件类 export class AppComponent { onChange(event: { name: string }) { console.log(event) } }
2.1 Overview
Dependency injection (Dependency Injection), referred to as
DI, is a design principle in object-oriented programming that is used to reduce the coupling between codes Degree
class MailService { constructor(APIKEY) {} } class EmailSender { mailService: MailService constructor() { this.mailService = new MailService("APIKEY1234567890") } sendMail(mail) { this.mailService.sendMail(mail) } } const emailSender = new EmailSender() emailSender.sendMail(mail)
EmailSender class must use the
MailService class when running,
EmailSender class depends on the
MailService class,
The MailService class is a dependency of the
EmailSender class.
MailService class changes the parameter delivery method, the writing method in the
EmailSender class will also change
class EmailSender { mailService: MailService constructor(mailService: MailService) { this.mailService = mailService; } } const mailService = new MailService("APIKEY1234567890") const emailSender = new EmailSender(mailService)when instantiating the
EmailSender class When injecting its dependencies into the interior of the class in the form of
constructor constructor parameters, this way of writing is dependency injection.
MailService Changes to the code in the class will no longer affect the
EmailSender class
2.2 DI Framework
Angular has its own
DI framework, which hides the process of implementing dependency injection. For developers, only Complex dependency injection functionality can be used with very simple code.
Angular's
DI framework:
Dependency: The instance object on which the component depends, service instance object
Token: Get the identifier of the service instance object
Injector: Injector, responsible for creating and maintaining instance objects of service classes and injecting service instance objects into components.
Provider: Configure the object of the injector, specify the service class to create the service instance object and obtain the identifier of the instance object.
2.2.1 InjectorsInjectors
import { ReflectiveInjector } from "@angular/core" // 服务类 class MailService {} // 创建注入器并传入服务类 const injector = ReflectiveInjector.resolveAndCreate([MailService])
const mailService = injector.get(MailService)
const mailService1 = injector.get(MailService) const mailService2 = injector.get(MailService) console.log(mailService1 === mailService2) // true
const injector = ReflectiveInjector.resolveAndCreate([MailService]) const childInjector = injector.resolveAndCreateChild([MailService]) const mailService1 = injector.get(MailService) const mailService2 = childInjector.get(MailService) console.log(mailService1 === mailService2) // false
const injector = ReflectiveInjector.resolveAndCreate([MailService]) const childInjector = injector.resolveAndCreateChild([]) const mailService1 = injector.get(MailService) const mailService2 = childInjector.get(MailService) console.log(mailService1 === mailService2) // true
2.2.2 ProviderProvider
const injector = ReflectiveInjector.resolveAndCreate([ { provide: MailService, useClass: MailService } ])
const injector = ReflectiveInjector.resolveAndCreate([ { provide: "mail", useClass: MailService } ]) const mailService = injector.get("mail")
useValue
const injector = ReflectiveInjector.resolveAndCreate([ { provide: "Config", useValue: Object.freeze({ APIKEY: "API1234567890", APISCRET: "500-400-300" }) } ]) const Config = injector.get("Config")
Programming Video! !
The above is the detailed content of Take you to understand component communication and dependency injection in Angular. For more information, please follow other related articles on the PHP Chinese website!