简体   繁体   English

RequireJS:无法读取回调中未定义的属性

[英]RequireJS: Cannot read property of undefined in callback

My application loads html using html() method from jQuery. 我的应用程序使用jQuery中的html()方法加载html。 I'm having a problem with running a module tabs created through RequireJS in the callback function switchScreenCallback() . 我在运行通过RequireJS在回调函数switchScreenCallback()创建的模块tabs遇到问题。 I tried different variants of making this and I'm not sure if the missing reference is caused by RequireJS or the callback. 我尝试了不同的制作方法,但不确定是否丢失的引用是由RequireJS或回调引起的。 The error always is: 该错误始终是:

Uncaught TypeError: Cannot read property 'switchTab' of undefined

I'm looking for a solution and I can provide a fuller code example if needed. 我正在寻找解决方案,如果需要,我可以提供更完整的代码示例。 How to make sure the tabs object is not undefined ? 如何确保tabs对象不是undefined

main.js: main.js:

requirejs.config({
    paths: {
        'jquery': '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min',
        'jqueryui': '//code.jquery.com/ui/1.11.4/jquery-ui',
        'viewObject': 'view',
        'script': 'script',
        'tabs': 'tabs',
    },
    shim: {
        jqueryui: {
            deps: ['jquery']
        },
        jquery: {
            exports: '$'
        },
        viewObject: {
            deps: ['jquery', 'jqueryui','tabs'],
            exports: 'viewObject'
        },
        tabs: {
            deps: ['jquery', 'jqueryui'],
            exports: 'tabs'
        }
    }
});
requirejs(['script']);

view.js: view.js:

define(['jquery', 'jqueryui', 'tabs'], function($,tabs) {
    console.log("tabs test 1",tabs === undefined);// returns true
    function callWhenReady(selector, callback, scope, params) {
        if ($(selector).closest('body').length) {
            callback.call(scope, params);
        }
        else {
            setTimeout(function() {
                callWhenReady(selector, callback, scope, params);
            }, 1);
        }
    }
    var viewObject = {
        switchScreenCallback: function(event) {
            console.log("ViewObject: SwitchScreenCallback",event);
            console.log("tabs test 2",tabs === undefined);// returns true
            console.log("tabs test 3",event.data.tabs === undefined);// returns true
            tabs.switchTab(event.data.tab);
            //event.data.tabs.switchTab(event.data.tab);
        },
        switchScreen: function(event) {
            $('#maincontainer').load(event.data.screen + '.html');
            callWhenReady('#' + event.data.screen, viewObject.switchScreenCallback, viewObject, event);     
        }
    }
    return {
        "viewObject": viewObject
    }
});

tabs.js: tabs.js:

define(['jquery', 'jqueryui'], function($) {
    var tabs = {
        switchTab: function(tab) {
            $('#tabs').tabs('select', tab);
        }
    }
    console.log("tabs created");
    return {
        "tabs":tabs
    }
});

Take a closer look at view.js: 仔细看看view.js:

define(['jquery', 'jqueryui', 'tabs'], function($,tabs) {
    ...
});

you are loading 3 dependencies, 'jquery', 'jqueryui', 'tabs' into 2 variables ($, tabs) . 您正在将3个依赖项'jquery', 'jqueryui', 'tabs'加载到2个变量($, tabs) So tabs is the result of jqueryui , which is undefined. 因此, tabsjqueryui的结果,它是未定义的。 the return value of tabs is not assign to anything. tabs的返回值未分配给任何东西。

try that instead: 尝试代替:

define(['jquery', 'jqueryui', 'tabs'], function($, dummy, tabs) {
        ...
    });

or just change the dependency order: 或只是更改依赖顺序:

define(['jquery', 'tabs', 'jqueryui'], function($, tabs) {
        ...
    });

FYI: you already declared jqueryui as a dependency for tabs (in the shim section), and he returns nothing, so i don't see any reason to call for it again in view.js (and same goes for Jquery if you don't actually use it in this code block) 仅供参考:您已经将jqueryui声明为tabs的依赖项(在shim部分中),并且他不返回任何内容,因此我认为没有任何理由在view.js中再次调用它(如果您不这样做,则对于Jquery也是一样)在此代码块中实际使用它)

I think this is the problem: 我认为这是问题所在:

define(['jquery', 'jqueryui', 'tabs'], function($,tabs) {

The above code will assign $ to jquery and jqueryui to tabs 上面的代码将$分配给jquery并将jqueryui分配给tabs

change it to: 更改为:

define(['jquery', 'jqueryui', 'tabs'], function($, $ui, tabs) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将angularJS与requireJS一起使用 - 无法读取未定义的属性“模块” - Using angularJS with requireJS - cannot read property 'module' of undefined DataTables+RequireJS:无法读取未定义的属性“默认值” - DataTables+RequireJS: Cannot read property 'defaults' of undefined Requirejs + Backbone Uncaught TypeError:无法读取未定义的属性“ each” - Requirejs + Backbone Uncaught TypeError: Cannot read property 'each' of undefined Requirejs + Backbone Uncaught TypeError:无法读取未定义的属性“Model” - Requirejs + Backbone Uncaught TypeError: Cannot read property 'Model' of undefined 未捕获的TypeError:无法读取未定义的属性'EventAggregator'(Backbone,Marionette,RequireJS) - Uncaught TypeError: Cannot read property 'EventAggregator' of undefined (Backbone, Marionette, RequireJS) 使用带角度的requireJS; 无法读取未定义的属性“模块” - Using requireJS with angular; Cannot read property 'module' of undefined RequireJS + Zepto +航点:无法读取未定义的属性“应用” - RequireJS + Zepto + Waypoints: Cannot read property 'apply' of undefined Chrome.webNavigation.onBeforeNavigate无法读取未定义的属性“回调” - Chrome.webNavigation.onBeforeNavigate Cannot read property 'callback' of undefined 将方法作为回调调用时无法读取未定义的属性 - Cannot read property of undefined when calling method as a callback Soundcloud Javascript SDK 3.0-回调无法读取未定义的属性“ connectCallback” - Soundcloud Javascript SDK 3.0 - Callback cannot read property 'connectCallback' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM