使用 Browserify 在 Node JS 项目中包含 JQuery Mobile [英] Including JQuery Mobile in a Node JS project with Browserify

查看:36
本文介绍了使用 Browserify 在 Node JS 项目中包含 JQuery Mobile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 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'); 

它有效.当我尝试添加 jQuery mobile 时出现问题,或者使用 require:

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>

我遇到了同样的错误:

未捕获的类型错误:无法设置未定义的属性 '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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆