在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并访问它,但我的问题是,我们不能将类级别属性分配给它的子组件吗?
答案 0 :(得分:2)
嗯,不是那样你不能。问题是你要在类原型中定义项目 - 这意味着你不能对那里的实例做任何特定的事情。
那是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);
}
});
通常,要小心在类级别属性中添加对象,因为它们将是原型上的属性,并在该类的所有实例之间共享。