Angular元件間怎麼進行通訊?依賴注入是什麼?以下這篇文章帶大家簡單了解元件通訊的方法,並介紹一下依賴注入,希望對大家有幫助!
1.1 傳送資料
##<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:html;toolbar:false;"><app-favorite [isFavorite]="true"></app-favorite></pre><div class="contentsignin">登入後複製</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">// favorite.component.ts
import { Input } from &#39;@angular/core&#39;;
export class FavoriteComponent {
@Input() isFavorite: boolean = false;
}</pre><div class="contentsignin">登入後複製</div></div>
注意:在屬性的外面加
[]
[] 表示綁定普通值,在元件內接收後是字串類型。 【相關教學推薦:《
angular教學》】
#1.2 元件向外部傳遞資料
需求:在子元件中透過點擊按鈕將資料傳遞給父元件
<!-- 子组件模板 --> <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. 依賴注入
2.1 概述
Dependency Injection ) 簡稱
DI,是物件導向程式設計中的一種設計原則,用來減少程式碼之間的耦合度數
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 類別運行時要使用MailService 類,EmailSender 類別依賴
MailService 類, MailService
EmailSender 類別的依賴項。
以上寫法的耦合度太高,程式碼並不健全。如果
MailService
EmailSender 類別中的寫法也要跟著改變
class EmailSender { mailService: MailService constructor(mailService: MailService) { this.mailService = mailService; } } const mailService = new MailService("APIKEY1234567890") const emailSender = new EmailSender(mailService)
在實例化
EmailSender 類時將它的依賴項透過透過依賴注入降了程式碼之間的耦合度,增加了程式碼的可維護性。
MailService
框架
有自己的DI
框架,它將實作依賴注入的過程隱藏了,對於開發者來說只需使用很簡單的程式碼就可以使用複雜的依賴注入功能。
的DI 框架中有四個核心概念:
:取得服務實例物件的識別碼
:配置注入器的對象,指定建立服務實例物件的服務類別和取得實例對象的識別。
2.2.1 注入器Injectors
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
const injector = ReflectiveInjector.resolveAndCreate([ { provide: MailService, useClass: MailService } ])
const injector = ReflectiveInjector.resolveAndCreate([ { provide: "mail", useClass: MailService } ]) const mailService = injector.get("mail")
const injector = ReflectiveInjector.resolveAndCreate([ { provide: "Config", useValue: Object.freeze({ APIKEY: "API1234567890", APISCRET: "500-400-300" }) } ]) const Config = injector.get("Config")
以上是帶你了解Angular中的元件通訊和依賴注入的詳細內容。更多資訊請關注PHP中文網其他相關文章!