標籤:

【MMR-A】Angular 去除對 Metadata Reflection API 的依賴

類別

必要重構/Meaningful Marvelous Refactor

API 更新/API Change Warning

置信度

Announcement

起始時間

2017-08-12

內容

相比於 AngularJS 而言,Angular 設計上的一大改進之處便是通過 Annotation 機制,極大地簡化配置相關過程,並去除對全局 API 的依賴(Tree-Shaking 友好)。

最初基於 AtScript 的實現中,元數據的配置基於 AtScript 的 Annotation 特性實現,並通過相應的屬性獲取。

在 Angular 遷移到 TypeScript 之後,相應的實現改為基於 ECMAScript Proposal 中的 Decorator 特性(Stage 2/Stage 0)實現,通過 Metadata Reflection API 進行元數據的設置與獲取。

隨著 AOT 模式的推出,在 AOT 模式下,元數據將直接編譯為類的靜態屬性(雖然事實上並不需要用到元數據),從而避免依賴額外的 Polyfill 以及提升性能。

在最新的 5.0.0-beta.3 版本中,ReflectiveInjector 已不被推薦使用(將在 v6 中廢棄),並增加了不依賴於 Metadata Reflection API 的 StaticInjector。所以基於 ReflectiveInjector 的注入(例如 Platform Providers)將不再支持基於類型標註的依賴配置,需要主動指定依賴。

隨後的 5.0.0-beta.4 (暫定)版本也去除了 Decorator 中對 Metadata Reflection 的依賴,而改用直接添加隱藏屬性的方式存儲元數據。同時,原有的 ES5 鏈式 API 被廢棄,Public API 中的提供的配置方式僅剩下 Decorator 一種。

原有的鏈式 API 示例:

app.AppComponent =n ng.core.Component({n selector: my-app,n template: <h1>Hello Angular</h1>n })n .Class({n constructor: function() {}n });n

目前的版本中,僅剩 JIT 模式下基於 TypeScript 中 emitDecoratorMetadata 選項的構造函數參數類型仍然需要用到 Metadata Reflection API(然而並不歸 Angular 控制)。

相關鏈接

  • Angular 增加 StaticInjector 支持的 Commit:feat(core): Create StaticInjector which does not depend on Reflect po… · angular/angular@d9d00bd
  • Angular 不推薦 ReflectiveInjector 的 Commit:perf: switch angular to use StaticInjector instead of ReflectiveInjector · angular/angular@fcadbf4
  • Angular 廢棄鏈式 API 支持並去除 Decorator 對 Metadata Reflection API 依賴的 Commit:perf(core): Remove decorator DSL which depends on Reflect · angular/angular@cac130e

推薦閱讀:

TAG:Angular? |