JavaScript - 使用 requireJS 加载 Bluebird 模块

标签 javascript module requirejs bluebird

我正在运行一个纯 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/

相关文章:

javascript - 使用 breeze 和 requireJS

javascript - 如何通过 RequireJS 加载 jQuery Migrate for jQuery?

javascript - 如果重复单击同一按钮,HTML 音频不会循环播放

javascript - 在同一空间中的两个 HTML 元素之间切换可见性

module - 引用兄弟模块(例如 Diesel 生成的模式)的正确方法是什么?

python - DataFrame 对象没有属性 'sample'

javascript - Meteor JS 共享方法和对象

javascript - Node.js 和 Backbone.js 的集成

Angular 2 : export component on a module and import and use it inside a module

javascript - 使用 RequireJS 加载 Youtube Iframe API