本文作為學(xué)習(xí)的感悟記錄,閱讀本文之前需要熟讀相關(guān)的官方文檔。
- declearable(components, directives, pipes)的作用域由NgModule來(lái)控制,而service的作用域由分層依賴注入機(jī)制來(lái)控制。
- 每一個(gè)component實(shí)例都有一個(gè)注入器,因此一個(gè)component tree對(duì)應(yīng)了一個(gè)與之平行的injector tree。相關(guān)文檔
組件的注入器可能是一個(gè)injector tree中更高級(jí)的祖先注入器的代理(如果某個(gè)component本身沒(méi)有
providers
),但這只是提升效率的實(shí)現(xiàn)細(xì)節(jié),我們不用在乎這點(diǎn)差異,在你的腦海里只要想象成每個(gè)組件都有自己的注入器就可以了。
- root module和其他eagerly loaded module的providers都注冊(cè)在application root injector,因此在其中一個(gè)NgModule中注冊(cè)的service是全局共享的(相關(guān)文檔)。(而在lazy loaded module中注冊(cè)的service則僅僅局限于這個(gè)模塊,后面會(huì)談到)
注入器樹(shù)
可以大致理解為最頂層是platform的注入器,然后下一層是application root injector,然后下面才是組件注入器樹(shù)。(在Platform Injector中注冊(cè)的是Angular內(nèi)部使用的一些服務(wù),開(kāi)發(fā)者一般不需要與它打交道)(一個(gè)頁(yè)面最多有1個(gè)Platform,一個(gè)Platform中可以有多個(gè)Application,但是絕大多數(shù)時(shí)候我們只需要?jiǎng)?chuàng)建一個(gè)Application)
包含lazy loaded module的注入器樹(shù)
每一個(gè)lazy loaded module有一個(gè)自己的注入器,這個(gè)注入器繼承上一個(gè)level的module注入器。最高level是eagerly loaded module,下一level是在eagerly loaded modules的路由定義中l(wèi)oadChildren指定的那些lazy modules,再下一level就是上一層lazy modules的路由定義中l(wèi)oadChildren指定的那些lazy modules,依此類(lèi)推。
每一層的module injector下面就是一顆(與component tree相同結(jié)構(gòu)、一一對(duì)應(yīng)的)injector tree。
我們可以將provider注冊(cè)在module injector中,也可以注冊(cè)在injector tree的某一個(gè)節(jié)點(diǎn)上。 相關(guān)文檔
- NgModule的providers注冊(cè)的順序:先注冊(cè)imported modules中的providers(按照imports數(shù)組中的順序),再注冊(cè)NgModule.providers數(shù)組中的provider,如果注冊(cè)了重復(fù)的provider token,后注冊(cè)的覆蓋先注冊(cè)的。相關(guān)文檔
- @Injectable的作用是在編譯時(shí)讓TypeScript編譯器將constructor中的依賴信息保存在class metadata中,運(yùn)行時(shí)注入器通過(guò)metadata中的信息來(lái)決定注入什么。如果service1需要在constructor中注入service2但service1卻沒(méi)有任何裝飾器,那么TypeScript編譯器就不會(huì)保存service1的依賴信息,那么在運(yùn)行時(shí)創(chuàng)建service1的時(shí)候就會(huì)報(bào)錯(cuò)(注入器不知道要注入什么)。相關(guān)文檔
- 除了可以用
被注入的class本身
或InjectionToken作為Provider token以外,使用class-interface也能獲得諸多好處(為編輯器提供editor tooling,narrowing interface有助于解耦,占用資源少,可以用來(lái)注入父組件)。 - 如果attribute directive被應(yīng)用在component element上時(shí)(
<my-component my-directive1 my-directive2></my-component>
)這些指令與組件共用同一個(gè)注入器。因此我們可以用attribute directive的providers array
來(lái)"擴(kuò)展"compoent的providers array
。這篇文章將attribute directive與component之間的協(xié)作寫(xiě)得很清楚了。
還有很多細(xì)節(jié)沒(méi)有搞清楚,比如:
- @Host究竟能查找到哪一個(gè)注入器(其表現(xiàn)與官方文檔說(shuō)明的不太一樣,經(jīng)過(guò)實(shí)驗(yàn)發(fā)現(xiàn)它能找到parent element上注冊(cè)的providers、parent component上的viewProviders,卻找不到parent component上的providers,存在transclusion的時(shí)候又復(fù)雜一些)
- 為什么在同一個(gè)element上的component、directive能共用同一個(gè)注入器
- 注入PlatformRef,ApplicationRef,NgModuleRef,Injector得到的對(duì)象中都有與injector相關(guān)的屬性,這些injector有什么聯(lián)系。
- 如果有一個(gè)directive掛載在一個(gè)普通element上,并且這個(gè)directive有providers數(shù)組,那么是否會(huì)在這個(gè)element上創(chuàng)建一個(gè)新的injector。
- 對(duì)于一個(gè)component來(lái)說(shuō),它的host element和host component分別是什么。
這些問(wèn)題目前還沒(méi)有在網(wǎng)上找到答案,可能需要研究源碼才能解答了。
參考資料
https://stackoverflow.com/questions/36455305/accessing-root-angular-2-injector-instance-globally
https://blog.thoughtram.io/angular/2015/08/20/host-and-visibility-in-angular-2-dependency-injection.html
https://angular-2-training-book.rangle.io/handout/di/angular2/the_injector_tree.html (此頁(yè)面尾部有一個(gè)很好用的在線例子)
https://medium.com/@ttemplier/component-composition-in-angular2-part-1-33f50f402906
https://stackoverflow.com/questions/44530654/angular-service-injecting-dynamic-component
https://stackoverflow.com/questions/45345163/angular-compiler-options