解决requirejs循环依赖问题

一.问题:A依赖B(即A引用B且调用B中的方法),B也依赖A,这即为循环依赖,那么,当B调用A中的方法时,会发现A为undefined,这就是循环依赖导致的问题。
二.解决循环依赖的方法:
1.用scope模式传参方式;
2.用pubsub解耦;
3.用require(“A”)的方式:
3.1.enterlib-controller.js(即为A)中引用了enterlib-view.js(即为B)中的init方法
这里写图片描述
3.2.enterlib-view.js中也想引用enterlib-controller.js中的test方法,这时,若是在enterlib-view.js中用define([“../controller/enterlib-controller”], function(Controller) {
Controller.test();
})
这种写法,会发现Controller为undefined,这是循环引用导致的,解决办法:
在enterlib-view.js中写:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/xujiezi/article/details/51077362