我正在尝试将项目从构造函数添加到 Sencha Touch (2.0) 面板子类。下面的代码:
Ext.define("MyApp.view.Icon", {
extend: "Ext.Panel",
config: {
layout: "vbox" //ensures caption appears directly below image
},
constructor: function(cfg) {
this.add(
//First we add the icon image
{
xtype: "image",
src: cfg.src,
width: cfg.width,
height: cfg.height
},
//Then we add the icon caption
{
xtype: "panel",
html: cfg.caption
}
);
return this;
}
});
var anIcon = Ext.create("MyApp.view.Icon", {
src: "http://placehold.it/80",
width: 100,
height: 100,
caption: "My Icon"});
这样做会给我错误:
Uncaught TypeError: Cannot call method 'has' of null
它似乎起源于this.add()
. 我也试过this.self.add()
了,还是不行。有没有办法从构造函数中插入元素?