【问题标题】:how can access require.js module's function?如何访问 require.js 模块的功能?
【发布时间】:2013-06-05 09:30:08
【问题描述】:

我想使用 a.js 模块的 test();

我的方法: - a.js -

define(function (require, exports, module) {
    function test(val) {
        alert(val);
    }
    window.test = test;
}

还有其他方法吗? 对不起,我的英语很差,希望你能理解,谢谢!

【问题讨论】:

    标签: javascript requirejs js-amd


    【解决方案1】:

    您通常不应该从 RequireJS 模块中分配给全局变量(即在 window 上设置属性)。

    /path/test.js

    define(function() {
        function test(val) {
            alert(val);
        }
        return test;
    }
    

    /path/app.html

    <!-- import requirejs ... -->
    
    <script>
    // Use the return value from the "test" module.
    require(["test"], function(testFn) {
        testFn("hello");
    });
    </script>
    

    【讨论】:

    • 清清楚楚,3Q很厉害!你好帅!
    • @user1817804 终于有人说出来了。
    猜你喜欢
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2016-02-27
    相关资源
    最近更新 更多