Angular框架里两个模块的互相依赖
如果把思路放寬一點,把狹義的死鎖場景中對資源的并發(fā)請求,拓寬到編程中的依賴管理,那么我們可以構(gòu)造一個循環(huán)依賴的場景,即模塊A和模塊B互相依賴。
可以很容易地用一個不到20行的Angular應(yīng)用來模擬循環(huán)依賴的場景。
執(zhí)行這個Angular應(yīng)用,遇到Angular框架報的錯誤信息:
VM17 angular.js:14802 Error: [injector:cdep]Circulardependencyfound:service1<?service2<?service1http://errors.angularjs.org/1.6.9/injector:cdep] Circular dependency found: service1 <- service2 <- service1 http://errors.angularjs.org/1.6.9/injector:cdep]Circulardependencyfound:service1<?service2<?service1http://errors.angularjs.org/1.6.9/injector/cdep?p0=service1%20%3C-%20service2%20%3C-%20service1
at VM17 angular.js:116
at getService (VM17 angular.js:5041)
at injectionArgs (VM17 angular.js:5074)
at Object.instantiate (VM17 angular.js:5120)
at Object. (VM17 angular.js:4955)
at Object.invoke (VM17 angular.js:5108)
at Object.enforcedReturnValue [as $get] (VM17 angular.js:4939)
at Object.invoke (VM17 angular.js:5108)
at VM17 angular.js:4893
at getService (VM17 angular.js:5049)
按照在代碼中的出現(xiàn)順序,首先執(zhí)行service1的注入。既然是第一次注入,因此代碼第5039行cache里肯定不存在service1對應(yīng)的實例,所以進入5045行的else分支,調(diào)用factory方法進行service1的實例化。
因為service1依賴于service2,所以service1實例化的時候觸發(fā)了service2的實例化:
而service2又依賴于service1,此時Angular在cache里檢測到service1還處于正在實例化的階段,為了避免這種循環(huán)依賴,Angular框架直接報exception.
要獲取更多Jerry的原創(chuàng)文章,請關(guān)注公眾號"汪子熙":
總結(jié)
以上是生活随笔為你收集整理的Angular框架里两个模块的互相依赖的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言 error C4996: �
- 下一篇: CloudFoundry命令行安装和卸载