【问题标题】:How to give config property to a view Item in Extjs 6?如何将配置属性赋予 Extjs 6 中的视图项?
【发布时间】:2016-09-29 10:20:18
【问题描述】:

在 Extjs 6 中,如何通过绑定或直接分配将配置属性直接分配给视图?

Ext.define('App.view.main.Main', {
extend: 'Ext.Container',

config: {
    btnLabel: 'MyButton'
},

someOtherProperty:'xyz',

items: [{
    xtype:'button',

    //text:this.someOtherProperty,
    //text:this.btnLabel,
    //text:this.getBtnLabel(),

    //here accessing this throws error, while loading this refers to windows object and not the class... 
    width:'150px'
    }],
});

我可以将属性移动到 ViewModel 并访问它,但我的问题是,我们不能将类级别属性分配给它的子组件吗?

【问题讨论】:

    标签: javascript extjs extjs5 extjs6 extjs6-modern


    【解决方案1】:

    嗯,不是那样的,你不能。问题是你在类原型中定义项目——这意味着你不能对那里的实例做任何特定的事情。

    这就是initComponent 的用途。我通常在那里定义我的items 属性,明确用于此目的:

    Ext.define('App.view.main.Main', {
      extend: 'Ext.Container',
      config: {
        btnLabel: 'MyButton'
      },
      someOtherProperty:'xyz',
    
      initComponent: function() {
        // The config properties have already been transferred to the instance
        // during the class construction, prior to initComponent being called.
        // That means we can now call this.getBtnLabel()
        this.items = [
          { xtype: 'button',
            text: this.getBtnLabel(),
            width:'150px'
        }]
    
        // Call the parent function as well. NB: After this, items won't be
        // a simple array anymore - it gets changed into a collection of
        // components.
        this.callParent(arguments);
      }
    });
    

    一般来说,将对象作为类级别的属性添加时要小心,因为它们将是原型上的属性并在该类的所有实例之间共享。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      相关资源
      最近更新 更多