我有一个要求,我需要将模块列表传递给插件,并让它加载模块并执行一些工作.如果我传递了一个无法加载的模块,我应该报告错误并转到列表的其余部分.我被困了,因为我无法弄清楚如何
define("my/thing", [], function() { return 'thing'; }); define("my/loader", [], function() { return { load: function(mid, require, callback) { console.log('inside load', arguments); // is there some way to recover when this require fails // or some other technique I can use here? try { require([mid], function(mod) { console.log('inside require, mod=', mod); callback(mod); }); } catch (error) { // never gets here, when the require fails everything just stops console.log(error); callback("failed to load " + mid); } } } }); require(["my/loader!my/thing"], function(loaded) { console.log('loaded', loaded); }); require(["my/loader!my/thing2"], function(loaded) { console.log('loaded', loaded); });如果您严格要求忽略无效或有故障的模块并继续下一个模块,请在将它们放入require语句之前使用 dojo/_base/lang::exists():
require(['/dojo/_base/lang', 'dojo/text!my/thing2'], function(lang, myThing2) { if(lang.exists(myThing2)) { //success } else { //failure } });