使用Browserify在Node JS项目中包含JQuery Mobile [英] Including JQuery Mobile in a Node JS project with Browserify
问题描述
我正在编写一个Node JS应用程序,我需要jQuery UI和jQuery Mobile。我正在使用Browserify将模块打包到一个js文件中。
I'm writing a Node JS application where I need both jQuery UI and jQuery Mobile. I'm using Browserify to pack the modules in a single js file.
我有以下代码在我的项目中包含jQuery和jQuery UI。
I have the following code to include jQuery and jQuery UI in my project.
var jQuery = require('jquery');
require('jquery-ui-browserify');
它有效。当我尝试使用require添加jQuery mobile时出现问题:
And it works. Problems arise when I try to add jQuery mobile, either with a require:
require('./lib/jquery.mobile-1.4.0.min.js');
或带有脚本标签
<script src="/lib/jquery.mobile-1.4.0.min.js" type="text/javascript"></script>
我得到同样的错误:
未捕获的TypeError:无法设置属性'mobile'未定义。
"Uncaught TypeError: Cannot set property 'mobile' of undefined".
我明白我得到了这个错误,因为jQuery mobile在Window中查找jQuery对象并且找不到它,但是如何修复它? npm上没有jQuery移动模块。
I understand that I get this error because jQuery mobile looks for the jQuery object in Window and does not find it, but how to fix it? There is no jQuery mobile module on npm.
推荐答案
这是一个使用 browserify-shim 和jquery + jquery mobile:
https://gist.github.com/mchelen/a8c5649da6bb50816f7e
Here is a simple runnable demo using browserify-shim and jquery + jquery mobile: https://gist.github.com/mchelen/a8c5649da6bb50816f7e
最重要的一行是:
package.json
"browserify-shim": {
"jquery": "$",
"jquery-mobile" : { "exports": "$.mobile", "depends": [ "jquery:$" ] }
},
entry.js
var $ = require('jquery');
$.mobile = require('jquery-mobile');
这篇关于使用Browserify在Node JS项目中包含JQuery Mobile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!