修改 Angular Component 构造函数参数被认为是 breaking change
修改構(gòu)造函數(shù)參數(shù)被認(rèn)為是 breaking change:
Making any changes to the class constructor signature. Note that super calls need to be updated in classes extending ours.
如果我們?cè)跇?gòu)造函數(shù)里引入新的參數(shù),這被認(rèn)為是 breaking change:
對(duì)于升級(jí)到新次要版本以及之前通過使用較少參數(shù)調(diào)用 super() 構(gòu)造函數(shù)在其代碼庫(kù)中擴(kuò)展我們的服務(wù)的任何客戶,這將導(dǎo)致 breaking change(特別是編譯錯(cuò)誤),例如在以下示例中 :
export class CustomService extends SpartacusService {constructor(promotionService: PromotionService){super(promotionService); // <--------- wrong constructor signature/* customer's constructor logic here */}}正確做法:
// TODO(#10946): make CartItemContextSource a required dependencyconstructor(promotionService: PromotionService,// eslint-disable-next-line @typescript-eslint/unified-signaturescartItemContextSource: CartItemContextSource);/*** @deprecated since 3.1*/constructor(promotionService: PromotionService);constructor(protected promotionService: PromotionService,@Optional() protected cartItemContextSource?: CartItemContextSource) {}/* ... */method() {console.log(this.cartItemContextSource?.item$);}注意以下三點(diǎn):
(1) 添加 ?使新的構(gòu)造函數(shù)參數(shù)可選。否則,傳遞較少參數(shù)的客戶將收到編譯錯(cuò)誤。
(2) 在類的邏輯中,允許新的構(gòu)造函數(shù)參數(shù)為空或未定義。您可以通過使用可選鏈 (?.) 訪問新依賴項(xiàng)的任何屬性來實(shí)現(xiàn)此目的,例如 this.cartItemContextSource?.item$。如果不這樣做,擴(kuò)展我們的類并向 super() 構(gòu)造函數(shù)傳遞較少參數(shù)的客戶將在我們的邏輯中收到運(yùn)行時(shí)錯(cuò)誤,因?yàn)?this.cartItemContextSource 對(duì)象將是未定義的。
(3) 如果您的類可能未提供新的構(gòu)造函數(shù)依賴項(xiàng)(例如,依賴項(xiàng)服務(wù)不是providedIn:‘root’,或者在DOM中有條件地提供),則在構(gòu)造函數(shù)依賴項(xiàng)之前使用@Optional()。否則,當(dāng)沒有條件提供依賴時(shí),客戶將收到無法解析依賴的 Angular 運(yùn)行時(shí)錯(cuò)誤。在構(gòu)造函數(shù)依賴項(xiàng)之前使用 @Optional() 告訴 Angular 在無法注入值時(shí)優(yōu)雅地回退到 null。
除了上述要求,我們還鼓勵(lì)您執(zhí)行以下操作:
(1) 添加內(nèi)聯(lián)注釋,例如 // TODO(#ticket-number): make X a required dependency,以引用下一個(gè)主要版本的計(jì)劃工作。
(2) 在實(shí)現(xiàn)上方添加構(gòu)造函數(shù)的兩個(gè)替代聲明。 最上面的聲明必須是最新的。
這是因?yàn)?#xff0c;在使用 SSR 的生產(chǎn)構(gòu)建中,只有第一個(gè)聲明用于解決依賴關(guān)系。 將 @deprecated 自 X.Y 添加到您的 JSDoc 評(píng)論也很有幫助。 包含此內(nèi)容后,客戶的 IDE 可以警告他們正在使用的舊構(gòu)造函數(shù)簽名(參數(shù)較少)已被棄用,這可以促使他們盡早遷移到新簽名。
Using the Inject Decorator for Dependencie
將 @Inject 用于依賴項(xiàng)時(shí),不應(yīng)包含任何構(gòu)造函數(shù)聲明。 相反,您應(yīng)該只包含構(gòu)造函數(shù)定義。
當(dāng)您構(gòu)建庫(kù)時(shí)(例如,當(dāng)您運(yùn)行 ng build --prod core 時(shí)),ng-packagr 工具僅使用第一個(gè)構(gòu)造函數(shù)聲明來解析注入的依賴項(xiàng),而忽略構(gòu)造定義。 但是,構(gòu)造函數(shù)聲明中不支持 Inject 裝飾器,因此它不能用于解析那里的依賴關(guān)系。 如果你包含一個(gè)帶有依賴的構(gòu)造函數(shù)聲明,ng-packagr 工具將無法解析依賴,你會(huì)得到一個(gè)錯(cuò)誤,如下所示:
ERROR: Internal error: unknown identifier []
一個(gè)錯(cuò)誤的例子:
import { PLATFORM_ID } from '@angular/core'; /*...*/// Do not add any constructor declarations when using @Inject to resolve a dependencyconstructor(platformId: any, // this dependency will not be resolved, nor can it be fixed with @Inject, because the Inject decorator is not supported here!newService?: NewService) {}constructor(protected platformId: any,) {}constructor(@Inject(PLATFORM_ID) protected platformId: any,protected newService?: NewService) {}一個(gè)正確的例子:
import { PLATFORM_ID } from '@angular/core'; /*...*/constructor(@Inject(PLATFORM_ID) protected platformId: any,protected newService?: NewService) {}總結(jié)
以上是生活随笔為你收集整理的修改 Angular Component 构造函数参数被认为是 breaking change的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 180767676是哪里的dns
- 下一篇: 在阿里云 ECS 上配置 SSH