【发布时间】:2014-09-09 18:23:44
【问题描述】:
这里有很多类似的问题,包括堆栈溢出的答案,但没有一个对我有用,所以我在这里问你们。感谢大家的时间。
我最近开始使用带有 browserify 的 gulp,效果很好。 然后我尝试在前端使用 browserify:Backbone 和 Bootstrap3。
在我尝试需要 Bootstrap 附带的 js 文件之前,一切似乎都可以正常工作。我在我的 chrome 工具中收到一条错误消息:jQuery 未定义。
我尝试过填充它,但我对填充物感到非常困惑。我正在使用 jQuery 2.1.1,所以我不需要填充 jQuery,但它现在存在于 shim 中,因为我绝望并尝试了一切。这是我的 package.json 和 main.js 文件:
-------------package.json------
{
"name": "gulp-backbone",
"version": "0.0.0",
"description": "Gulp Backbone Bootstrap",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rob Luton",
"license": "ISC",
"devDependencies": {
"jquery": "^2.1.1",
"backbone": "^1.1.2",
"browserify": "^4.2.1",
"gulp": "^3.8.6",
"vinyl-source-stream": "^0.1.1",
"gulp-sass": "^0.7.2",
"gulp-connect": "^2.0.6",
"bootstrap-sass": "^3.2.0",
"browserify-shim": "^3.6.0"
},
"browser": {
"bootstrap": "./node_modules/bootstrap-sass/assets/javascripts/bootstrap.js",
"jQuery": "./node_modules/jquery/dist/jquery.min.js"
},
"browserify": {
"transform": ["browserify-shim"]
},
"browserify-shim": {
"jquery": "global:jQuery",
"bootstrap": {
"depends": [
"jQuery"
]
}
}
}
-------------------------- main.js ------------------- ---
var shim = require('browserify-shim');
$ = require('jquery');
var Backbone = require('backbone');
Backbone.$ = $;
var bootstrap = require('bootstrap');
/* the following logs fine if I comment out the bootstrap require, otherwise I get 'jQuery undefined' */
console.log(Backbone);
$(function() {
alert('jquery works');
});
【问题讨论】:
-
您可能对 github.com/twbs/bootstrap/pull/13904 中删除的 Bootstrap UMD 脚手架感兴趣
-
如果您不想使用 browserify-shim,请在 stackoverflow.com/questions/24978244/… 中查看我的回答
标签: javascript twitter-bootstrap-3 browserify browserify-shim