与Firebug / Chrome控制台中的require.js模块交互? [英] Interacting with require.js modules from the Firebug/Chrome console?

查看:123
本文介绍了与Firebug / Chrome控制台中的require.js模块交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用require.js。我已经成功地包装了jquery,一些插件和一些我自己的模块。我正在尝试与Firebug(或Google Chrome的JS控制台)中的模块(或jquery)进行交互,但我运气不错。

I'm just getting started with require.js. I have successfully wrapped jquery, some plugins, and a couple of my own modules. I'm trying to interact with my modules (or jquery) from Firebug (or Google Chrome's JS console), and I'm not having much luck.

什么是从控制台访问这些模块的正确方法是什么?

What is the correct way to access these modules from the console?

推荐答案

假设我们有模块/app/scripts/methodsModule.js返回一个几种方法:

Say we have module /app/scripts/methodsModule.js that returns a few methods:

define({
    someMethod: function() {
        // do stuff
    },
    anotherMethod: function() {
        // do some more stuff
    }
});

在我们的数据主文件/app/scripts/main.js中,我们有:

In our data-main file /app/scripts/main.js we have:

require(['methodsModule'], function(methods) {
    methods.someMethod() // call someMethod
    methods.anotherMethod() // call anotherMethod
})

一旦requireJS加载我们的数据-main,我们可以从javascript控制台命令行访问已经由requireJS加载的任何模块,如下所示:

Once requireJS loads up our data-main, we can access any modules that have already been loaded by requireJS from the javascript console command line like so:

>> methods = require('methodsModule'); // requireJS has module methodsModule stored
>> methods.someMethod() // call someMethod
>> methods.anotherMethod() // call anotherMethod

如果模块尚未通过调用加载require()或define(),我们必须传递自己的回调函数才能在模块加载后调用require函数:

If a module hasn't been loaded by a call to require() or define(), we have to pass our own callback for the require function to call after the module has been loaded:

>> myCB = function(methods) { methods.someMethod() }
>> require(['methodsModule'], myCB)

否则,requireJS会抛出一个错误,说模块有尚未加载..

Otherwise, requireJS throws an error saying that the module has not yet been loaded..

这篇关于与Firebug / Chrome控制台中的require.js模块交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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