Uncaught ReferenceError: RSVP is not defined , error while using require.js with rsvp
Uncaught ReferenceError: RSVP is not defined , error while using require.js with rsvp
我正在开发一个演示,展示使用 rsvp.js 在 promises 中的错误处理。一切似乎都很好,直到我在标签中将 CDN url 用于 rsvp.js。现在,由于我的应用程序中有 require.js 模块加载,我尝试通过 require js 语法加载 rsvp.js 模块。在 Chrome 网络选项卡中,我看到 rsvp.js 模块也已正确加载,但我在控制台中收到以下错误,
Uncaught ReferenceError: RSVP is not defined.
require(["bootstrap","js/rsvp"], function(bootstrap,rsvp) {
$(document).ready(function () {
function getEmployeeDetails() {
var radioValue1 = $("input[name='option1']:checked").val();
var requrl;
if (radioValue1 == "fail") {
requrl = "../../../testservice/getEmployeeIdss";
} else {
requrl = "../../../testservice/getEmployeeId";
}
return new RSVP.Promise(function (resolve, reject) {
$.ajax({
url: requrl,
success: function (response) {
try {
$('#successBoard').append("<b> <i> Ajax Call 1 Succeeded! </i> </b><br/>" + "Employee ID:" + response.stuId + "<br/>");
resolve(response.stuId);
} catch (e) {
reject(e);
}
},
error: function (xhr, ajaxOptions, thrownError) {
console.log('Ajax 1 failed : Rejecting Promise 1');
$('#errorBoard').append("<b> <i> Ajax 1 failed : Rejecting Promise 1</i> </b><br/>");
reject(thrownError);
}
});
});
}
您还没有定义 RSVP
,您已经包含了 rsvp 库但将它设置为变量 rsvp
而不是 RSVP
,所以当您调用 RSVP.Promise()
时它抛出错误。
基本用法通过 - https://github.com/tildeio/rsvp.js/-
var RSVP = require('rsvp');
var promise = new RSVP.Promise(function(resolve, reject){});
我查看了 source code of RSVP 并找到了这段代码:
/* global define:true module:true window: true */
if (typeof define === 'function' && define['amd']) {
define(function() { return RSVP; });
} else if (typeof module !== 'undefined' && module['exports']) {
module['exports'] = RSVP;
} else if (typeof platform !== 'undefined') {
platform['RSVP'] = RSVP;
}
第一个if
是RSVP检测到是AMD环境下的运行,注册为AMD模块。所以你不会有一个全局 RSVP
.
现在,在您的代码中,您需要 RSVP 并将其绑定到 rsvp
变量,全部小写。所以它可以作为 rsvp
而不是 RSVP
来访问。将其称为 rsvp
或将传递给 require
的函数中的变量名称更改为 RSVP
:
require(["bootstrap", "js/rsvp"], function (bootstrap, RSVP) {
请注意,引用 Bootstrap 没有意义,因为它会将自己安装为 jQuery 插件,因此您可以:
require(["js/rsvp", "bootstrap"], function (RSVP) {
我正在开发一个演示,展示使用 rsvp.js 在 promises 中的错误处理。一切似乎都很好,直到我在标签中将 CDN url 用于 rsvp.js。现在,由于我的应用程序中有 require.js 模块加载,我尝试通过 require js 语法加载 rsvp.js 模块。在 Chrome 网络选项卡中,我看到 rsvp.js 模块也已正确加载,但我在控制台中收到以下错误,
Uncaught ReferenceError: RSVP is not defined.
require(["bootstrap","js/rsvp"], function(bootstrap,rsvp) {
$(document).ready(function () {
function getEmployeeDetails() {
var radioValue1 = $("input[name='option1']:checked").val();
var requrl;
if (radioValue1 == "fail") {
requrl = "../../../testservice/getEmployeeIdss";
} else {
requrl = "../../../testservice/getEmployeeId";
}
return new RSVP.Promise(function (resolve, reject) {
$.ajax({
url: requrl,
success: function (response) {
try {
$('#successBoard').append("<b> <i> Ajax Call 1 Succeeded! </i> </b><br/>" + "Employee ID:" + response.stuId + "<br/>");
resolve(response.stuId);
} catch (e) {
reject(e);
}
},
error: function (xhr, ajaxOptions, thrownError) {
console.log('Ajax 1 failed : Rejecting Promise 1');
$('#errorBoard').append("<b> <i> Ajax 1 failed : Rejecting Promise 1</i> </b><br/>");
reject(thrownError);
}
});
});
}
您还没有定义 RSVP
,您已经包含了 rsvp 库但将它设置为变量 rsvp
而不是 RSVP
,所以当您调用 RSVP.Promise()
时它抛出错误。
基本用法通过 - https://github.com/tildeio/rsvp.js/-
var RSVP = require('rsvp');
var promise = new RSVP.Promise(function(resolve, reject){});
我查看了 source code of RSVP 并找到了这段代码:
/* global define:true module:true window: true */
if (typeof define === 'function' && define['amd']) {
define(function() { return RSVP; });
} else if (typeof module !== 'undefined' && module['exports']) {
module['exports'] = RSVP;
} else if (typeof platform !== 'undefined') {
platform['RSVP'] = RSVP;
}
第一个if
是RSVP检测到是AMD环境下的运行,注册为AMD模块。所以你不会有一个全局 RSVP
.
现在,在您的代码中,您需要 RSVP 并将其绑定到 rsvp
变量,全部小写。所以它可以作为 rsvp
而不是 RSVP
来访问。将其称为 rsvp
或将传递给 require
的函数中的变量名称更改为 RSVP
:
require(["bootstrap", "js/rsvp"], function (bootstrap, RSVP) {
请注意,引用 Bootstrap 没有意义,因为它会将自己安装为 jQuery 插件,因此您可以:
require(["js/rsvp", "bootstrap"], function (RSVP) {