RequireJS 正确处理 404 外部依赖
RequireJS properly handling 404 on external dependency
我不确定如何处理来自 RequireJS 的外部依赖项的 404。我在下面包含了我的 require.js 文件的示例:
requirejs.config({
paths: {
jquery: '/js/jquery-2.1.0.min',
script1: 'http://s3.amazonaws.com/script1.min',
script2: 'http://s3.amazonaws.com/script2.min',
},
shim: {
script1: {
deps:['jquery'],
exports: 'script1'
},
script2: {
deps:['script1'],
exports: 'script2'
}
}
});
requirejs([
'script1',
'script2'
]);
我希望能够处理外部依赖性下降或不可用,而无需 Script Error
。 Script Error
在那种情况下是正确的行为吗?我想一定有一种方法可以在出现 404 时不包含脚本。
require 可以接受一个附加参数用于错误回调,如下所示:
require(['script1', 'script2'], function (a, b) {
// Normal execution
}, function (err) {
// Error, such as a 404.
});
如果需要,您可以分离加载每个依赖项,这样您就可以在 script1 或 script2 404 时执行自定义错误处理。
require([], function () {
require(['script1'], function (a) {
// Do stuff with a
}, function (err) { });
require(['script2'], function (b) {
// Do stuff with b
}, function (err) { });
});
我不确定如何处理来自 RequireJS 的外部依赖项的 404。我在下面包含了我的 require.js 文件的示例:
requirejs.config({
paths: {
jquery: '/js/jquery-2.1.0.min',
script1: 'http://s3.amazonaws.com/script1.min',
script2: 'http://s3.amazonaws.com/script2.min',
},
shim: {
script1: {
deps:['jquery'],
exports: 'script1'
},
script2: {
deps:['script1'],
exports: 'script2'
}
}
});
requirejs([
'script1',
'script2'
]);
我希望能够处理外部依赖性下降或不可用,而无需 Script Error
。 Script Error
在那种情况下是正确的行为吗?我想一定有一种方法可以在出现 404 时不包含脚本。
require 可以接受一个附加参数用于错误回调,如下所示:
require(['script1', 'script2'], function (a, b) {
// Normal execution
}, function (err) {
// Error, such as a 404.
});
如果需要,您可以分离加载每个依赖项,这样您就可以在 script1 或 script2 404 时执行自定义错误处理。
require([], function () {
require(['script1'], function (a) {
// Do stuff with a
}, function (err) { });
require(['script2'], function (b) {
// Do stuff with b
}, function (err) { });
});