我正在运行一个纯 JavaScript 项目(不使用节点),我需要加载一个名为 Bluebird 的 Promises 模块。 我没有使用 JavaScript 加载模块的经验,过去我一直使用平均堆栈。
我在index.html中有以下内容:
<script data-main="assets/js/app/App.js" src="assets/js/vendor/require.js"></script>
<script src="assets/js/app/App.js"></script>
我在网上读到,在我的 App.js 中我应该有:
define(["bluebird"],function(Promise){
//load modules
});
问题是我不知道用什么来代替“//加载模块”来加载 bluebird.js。我尝试过使用:
define(["bluebird"],function(Promise){
Promise = require('bluebird');
});
但这有错误:
require.js:5 Uncaught Error: Mismatched anonymous define() module: function (Promise){
Promise = require('bluebird');
}
请注意,我确实尝试仅使用 var Promise = require('bluebird');
但这返回了 Uncaught Error :尚未为上下文加载模块名称“bluebird”:_ 。使用 require([])
最佳答案
最终我来自 requirejs 错误文档的原始代码是正确的,但我的 index.html 中存在问题,导致它失败。
包含 bluebird 的正确方法是:
require(['bluebird'], function (Promise) { //module loaded });
在 //module returned
中,您需要放置大量 App.js 代码。
如果 requirejs 正在加载它,您需要确保不包含 for bluebird。
关于JavaScript - 使用 requireJS 加载 Bluebird 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46582179/