【问题标题】:Require JS with Node and jQuery需要带有 Node 和 jQuery 的 JS
【发布时间】:2014-01-08 09:08:17
【问题描述】:

我正在使用 r.js 尝试通过在我的主应用程序文件上运行 r.js 来编译我的 require JS 应用程序:

$ r.js js/bootstrap.js

但是,我在使用 jQuery 时遇到了错误:

Error: Evaluating www/js/lib/jquery_plugins/slider.js as module "slider" failed with error: ReferenceError: jQuery is not defined

所以我知道这是因为在我的滑块插件中引用了 jQuery,但我认为这可以通过在我的 bootstrap.js 文件中添加 jQuery 作为 shim 来解决,该文件如下所示:

// Require JS bootstrap file
require.config({

    paths: {
        jquery: 'lib/jquery',
        backbone: 'lib/backbone',
        backboneLocalStorage: 'lib/backbone.localStorage',
        underscore: 'lib/underscore',
        util: 'lib/util',
        config: 'config',
        lang: 'lang',
        hammer: 'lib/hammer',
        moment: 'lib/jquery_plugins/moment',
        slider: 'lib/jquery_plugins/slider'
    },
    shim: {
        jquery: {
            exports: '$'
        },
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ['underscore'],
            exports: 'Backbone'
        },
        hammer: {
            deps: ['jquery'],
            exports: 'Hammer'
        }
    }
});

// load the app
require(['app'], function(App) {
    if(navigator.userAgent.indexOf("Chrome")) {
        // then I am running on my local host on chrome
        // so force device ready
        onDeviceReady();
    }
    document.addEventListener('deviceready', onDeviceReady, false);

    function onDeviceReady() {
        App.initialize();
    }
});

我尝试将导出更改为 jQuery 而不是 $,但这似乎没有任何改变。

【问题讨论】:

    标签: jquery node.js requirejs r.js


    【解决方案1】:

    在 shim 下,您没有指定滑块和时刻的依赖项。

    请检查什么滑块和时刻导出:)

    shim: {
            jquery: {
                exports: '$'
            },
            underscore: {
                exports: '_'
            },
            backbone: {
                deps: ['underscore'],
                exports: 'Backbone'
            },
            hammer: {
                deps: ['jquery'],
                exports: 'Hammer'
            },
            slider: {
                deps: ['jquery'],
                exports: 'slider'
            },
            moment: {
                deps: ['jquery'],
                exports: 'moment'
            }
        }
    

    【讨论】:

    • 我现在收到此错误Error: Evaluating www/js/lib/jquery.js as module "jquery" failed with error: ReferenceError: window is not defined,有什么想法吗?
    • 你能从 shim 中删除 jquery: { exports: '$' } 并检查吗?
    • 删除后仍然显示相同的错误。我也试过jquery: ['jquery'],然后说Shim config not supported in Node, may or may not work. Detected for module: underscore Shim config not supported in Node, may or may not work. Detected for module: backbone,然后我得到一个关于window is not defined inbackbone.localStorage.js的错误
    猜你喜欢
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2018-12-16
    • 2019-10-05
    • 1970-01-01
    相关资源
    最近更新 更多