Sencha touch 如何在同一视图senchatouch中访问下面定义的函数中初始化视图时声明的变量

Sencha touch 如何在同一视图senchatouch中访问下面定义的函数中初始化视图时声明的变量,sencha-touch,sencha-touch-2,Sencha Touch,Sencha Touch 2,我在视图的初始化部分声明了一个变量。当我尝试在面板中声明的函数中访问此变量时,我收到一个错误,即变量未定义。我尝试通过1>this.variablename 2>viewid.variablename…访问它,我做错了什么 Ext.define('app.view.location', { extend : 'Ext.Panel', xtype : 'location_d', id : 'locdetail',

我在视图的初始化部分声明了一个变量。当我尝试在面板中声明的函数中访问此变量时,我收到一个错误,即变量未定义。我尝试通过1>this.variablename 2>viewid.variablename…访问它,我做错了什么

Ext.define('app.view.location', {
            extend : 'Ext.Panel',
            xtype : 'location_d',
            id : 'locdetail',
            initialize : function() {
                loc = 'abc';
            },
            config : {
                layout : {
                    type : 'card'
                },
                scrollable : true,
                fullscreen : true,
                items : [{
                            xtype : 'panel',
                            html : 'Get Dir',
                            id : 'Dir',
                            cls : 'loc',
                            listeners : {
                                tap : {
                                    element : 'element',
                                    fn : function(m) {
                                        alert(this.loc); //gives me undefined variable error
                                    }
                                }
                            }
                        }]
            }
        });

您的代码中有一些错误

首先,您需要在配置中添加loc als变量

config : {
    layout : {
        type : 'card'
    },
    loc: null,
    scrollable : true,
    ...}
那么你应该和他的二传手定好位置

initialize : function() {
    this.setLoc("abc");
},
在tap listener中,您不能使用此,因为它引用的是内部面板而不是外部面板,请使用ComponenetManager获取外部面板

listeners : {
    tap : {
        element : 'element',
        fn : function(m) {
            alert(Ext.ComponentManager.get("locdetail").getLoc());
        }
    }
}
我试过了,它没有问题

完整代码:

Ext.define('app.view.location', {
    extend : 'Ext.Panel',
    xtype : 'location_d',
    id : 'locdetail',
    initialize : function() {
        this.setLoc("abc");
    },
    config : {
        layout : {
            type : 'card'
        },
        loc: null,
        scrollable : true,
        fullscreen : true,
        items : [{
            xtype : 'panel',
            html : 'Get Dir',
            id : 'Dir',
            cls : 'loc',
            listeners : {
                tap : {
                    element : 'element',
                    fn : function(m) {
                        alert(Ext.ComponentManager.get("locdetail").getLoc());
                    }
                }
            }
        }]
    }
});

Sencha Touch Class系统指南:

您的代码中有一些错误

首先,您需要在配置中添加loc als变量

config : {
    layout : {
        type : 'card'
    },
    loc: null,
    scrollable : true,
    ...}
那么你应该和他的二传手定好位置

initialize : function() {
    this.setLoc("abc");
},
在tap listener中,您不能使用此,因为它引用的是内部面板而不是外部面板,请使用ComponenetManager获取外部面板

listeners : {
    tap : {
        element : 'element',
        fn : function(m) {
            alert(Ext.ComponentManager.get("locdetail").getLoc());
        }
    }
}
我试过了,它没有问题

完整代码:

Ext.define('app.view.location', {
    extend : 'Ext.Panel',
    xtype : 'location_d',
    id : 'locdetail',
    initialize : function() {
        this.setLoc("abc");
    },
    config : {
        layout : {
            type : 'card'
        },
        loc: null,
        scrollable : true,
        fullscreen : true,
        items : [{
            xtype : 'panel',
            html : 'Get Dir',
            id : 'Dir',
            cls : 'loc',
            listeners : {
                tap : {
                    element : 'element',
                    fn : function(m) {
                        alert(Ext.ComponentManager.get("locdetail").getLoc());
                    }
                }
            }
        }]
    }
});

Sencha Touch类系统指南:

在这种情况下,最好将您的代码添加到帖子中,这样我们才能真正看到您正在尝试做什么。Ext.define('app.view.location',{extend:'Ext.Panel',xtype:'location_d',id:'locdetail',initialize:function(){loc abc';}config:{layout:{type:'card'},可滚动:true,全屏:true,items:[{xtype:'panel',html:'Get Dir',id:'Dir',cls:'loc',侦听器:{tap:{element:'element',fn:function(m){alert(this.loc);//给我未定义的变量错误}}},},},}]}在这种情况下,最好将您的代码添加到文章中,这样我们才能真正看到您正在尝试执行的操作。Ext.define('app.view.location',{extend:'Ext.Panel',xtype:'location_d',id:'locdetail',initialize:function(){loc abc';}config:{布局:{type:'card'},可滚动:true,全屏:true,项:[{xtype:'panel',html:'Get Dir',id:'Dir',cls:'loc',侦听器:{tap:{element:'element',fn:function(m){alert(this.loc);//给我未定义的变量error},},}]}非常感谢Lukas,这对我有效。非常感谢你的帮助,然后请接受此答案,以便其他用户知道它有效。非常感谢Lukas,这对我有效。非常感谢你的帮助,然后请接受此答案,以便其他用户知道它有效。