日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Angular框架里两个模块的互相依赖

發布時間:2023/12/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Angular框架里两个模块的互相依赖 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果把思路放寬一點,把狹義的死鎖場景中對資源的并發請求,拓寬到編程中的依賴管理,那么我們可以構造一個循環依賴的場景,即模塊A和模塊B互相依賴。
可以很容易地用一個不到20行的Angular應用來模擬循環依賴的場景。

<html ng-app="test"> <head><title>Angular Circular Dependency Example</title> </head> <body ng-controller="testController">Test </body> <script type="text/javascript">var module = angular.module('test', []);module.service('service1', function(service2) {});module.service('service2', function(service1) {});module.controller('testController', function(service1) {}); </script> </html>

執行這個Angular應用,遇到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)
按照在代碼中的出現順序,首先執行service1的注入。既然是第一次注入,因此代碼第5039行cache里肯定不存在service1對應的實例,所以進入5045行的else分支,調用factory方法進行service1的實例化。

因為service1依賴于service2,所以service1實例化的時候觸發了service2的實例化:

而service2又依賴于service1,此時Angular在cache里檢測到service1還處于正在實例化的階段,為了避免這種循環依賴,Angular框架直接報exception.

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

總結

以上是生活随笔為你收集整理的Angular框架里两个模块的互相依赖的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。