RT,這個問題困擾了我兩天,使用別人的例子、官網down下來的example都沒有問題。但是放到自己項目裏就 var $=require("jquery") 爲null。
後來發現,jquery本來沒有amd規範的,於是從官網上的example下來裏面的jquery拿過來直接用,最注意的是,在jquery裏面增加了有define這段,裏面有一個路徑要根據自己實際項目路徑進行調整
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
// Expose jQuery as module.exports in loaders that implement the Node
// module pattern (including browserify). Do not create the global, since
// the user will be storing it themselves locally, and globals are frowned
// upon in the Node module world.
module.exports = jQuery;
} else {
// Otherwise expose jQuery to the global object as usual
window.jQuery = window.$ = jQuery;
// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.
if ( typeof define === "function" ) {
define("lib/jquery/jquery/1.10.1/jquery-debug",[], function () { return jQuery; } );
}
}
define("lib/jquery/jquery/1.10.1/jquery-debug",[], function () { return jQuery; } );