作者:不要破网名_329 | 来源:互联网 | 2023-01-15 11:22
ImtryingtogetanapprunningthatusesbothAngularJSandRequireJS.Imhavingproblemsgetting
I'm trying to get an app running that uses both AngularJS and RequireJS. I'm having problems getting my OpenLayers lib to work in this setup.
我正在尝试运行使用AngularJS和RequireJS的应用程序。我在让我的OpenLayers lib在此设置中工作时遇到问题。
I set the main AMD-modules in the main.js:
我在main.js中设置了主要的AMD模块:
require.config(
{
baseUrl: 'scripts',
paths: {
// Vendor modules
angular: 'vendor/angular/angular',
openLayers: 'vendor/openlayers-debug',
other modules.....
},
shim: {
angular: {
exports: 'angular'
},
openLayers: {
exports: 'OpenLayers'
},
other modules....
}
}
);
require(['openLayers',
'angular',
'app',
'controllers/olMapController',
'directives/olMap',
other modules...
], function(OpenLayers) {
return OpenLayers;
}
);
Then in the angular controller I use for the initialisation of OpenLayers, I try to indicate that openlayers-debug.js is a dependency:
然后在我用于初始化OpenLayers的角度控制器中,我试着指出openlayers-debug.js是一个依赖:
define(['openLayers'],
function(OpenLayers) {
controllers.controller('olMapController', ['$scope', function(scope) {
console.log('Loaded olMapController. OpenLayers version: ' + OpenLayers.VERSION_NUMBER);
}]);
}
);
Well, this doesn't work. SOMETIMES the olMapController function is executed, but mostly not. The console then just displays an error stating:
嗯,这不起作用。有时执行olMapController函数,但大部分都没有。然后控制台只显示一个错误说明:
Error: [ng:areq] Argument 'olMapController' is not a function, got undefined
错误:[ng:areq]参数'olMapController'不是函数,未定义
So, I think OpenLayers hasn't finished loading yet, but somehow require thinks it has and continues loading code that depends on OpenLayers, in this case the olMapController. It then can't find its dependency, whereupon Angular returns this error message. Or something like that...? Sometimes something happens that makes OpenLayers load fast enought for it to be present when it is loaded as a dependency. What that is, I can't tell.
所以,我认为OpenLayers尚未完成加载,但不知何故需要认为它已经并继续加载依赖于OpenLayers的代码,在本例中是olMapController。然后它无法找到它的依赖关系,于是Angular返回此错误消息。或类似的东西...?有时发生的事情会使OpenLayers加载得很快,因为当它作为依赖项加载时它会存在。那是什么,我说不出来。
I left out other libraries and modules require and define to keep the code readable. I hope the example is still understandable.
我遗漏了其他库和模块需要并定义以保持代码可读。我希望这个例子仍然可以理解。
Any ideas on what I could do to get openlayers to load well? The error message disappears when I leave the ['openLayers']
dependency out of olMapController.
有什么想法可以让openlayers加载好吗?当我将['openLayers']依赖项从olMapController中删除时,错误消息消失。
Thanks in advance for any help.
在此先感谢您的帮助。
Best regards, Martijn Senden
最好的问候,Martijn Senden
2 个解决方案
5
Well, just for reference my final solution. The comment by angabriel set me off in the right direction.
好吧,仅供参考我的最终解决方案。安格布里尔的评论让我朝着正确的方向前进。
Like i said, I'm using the domReady module provided by RequireJS to bootstrap Angular. This is still being called to early, because OpenLayers isn't loaded yet when angular starts. RequireJS also provides a callback property in require.config. This is a function that is triggered when all the dependencies are loaded. So I used that function to require the angular bootstrap module. My config now looks like this:
就像我说的,我正在使用RequireJS提供的domReady模块来引导Angular。这仍然被提前调用,因为当角度开始时OpenLayers尚未加载。 RequireJS还在require.config中提供了一个回调属性。这是在加载所有依赖项时触发的函数。所以我使用该函数来要求角度引导模块。我的配置现在看起来像这样:
require.config(
{
baseUrl: 'scripts',
paths: {
// Vendor modules
angular: 'vendor/angular/angular',
domReady: 'vendor/domReady',
openLayers: 'vendor/openlayers-debug',
etcetera....
},
shim: {
angular: {
deps: ['jquery'],
exports: 'angular'
},
openLayers: {
exports: 'OpenLayers'
},
},
deps: ['angular',
'openLayers',
'app',
'domReady',
'controllers/rootController',
'controllers/olMapController',
'directives/olMap'
],
callback: function() {
require(['bootstrap']);
}
}
);
And the bootstrap module looks like this:
而bootstrap模块看起来像这样:
define(['angular', 'domReady', 'app'], function(angular, domReady) {
domReady(function() {
angular.bootstrap(document, ['GRVP']);
});
});
Thanks for the help. @angabriel: I upvoted your comment. It's not possible to accept a comment as an answer, is it? Your initial answer wasn't really the answer to my question, but the comment helped a lot...
谢谢您的帮助。 @angabriel:我赞成你的评论。接受评论作为答案是不可能的,是吗?你最初的答案并不是我问题的答案,但评论帮了很多...
Regards, Martijn