requirejs – 使用 jQuery 在 Chrome 出現 $ is not a function

如果這麼寫,會發生錯誤。

requirejs([
    'plugin/JS/jquery-1.11.3.min',
], function ($){
    console.log($); //undefined
})

這是因為 jQuery 已經 define 規範要使用 jquery 關鍵字來註冊,否則會產生錯誤。所以要先透過 path 設定路徑別名,改成

requirejs.config({
    paths : {
        jquery : "plugin/JS/jquery-1.11.3.min"
    }
})

requirejs([
    'jquery',
], function ($){
    console.log($);
})

 

參考資料 http://stackoverflow.com/questions/10131076/why-isnt-requirejs-passing-jquery-into-the-functions-alias

Comments

發表迴響