简体   繁体   English

无法将元素/组件附加到Extjs面板

[英]Can't append Element/Component to a Extjs Panel

I am trying to append a button to a panel. 我正在尝试将一个按钮附加到面板上。 First I create the panel, and render it to Ext.getBody() . 首先,我创建面板,并将其渲染到Ext.getBody() That seems to work fine, but then I want to render a button to that panel, and it gives this error : 这似乎工作正常,但随后我想向该面板渲染一个按钮,并且出现此错误:

Uncaught TypeError: Cannot read property 'dom' of null 未捕获的TypeError:无法读取null的属性“ dom”

Here is the code : 这是代码:

Ext.onReady(function () {

    var p = Ext.create('Ext.Panel',{
        renderTo:Ext.getBody(),
        html:'myPanel'
    })


    Ext.create('Ext.Button', {
        text: 'Click me!!!!',
        renderTo: p,
        handler: function() {
            alert('You clicked the button!')
        }
    });
});

Fiddle here : 在这里摆弄:

https://fiddle.sencha.com/#fiddle/694 https://fiddle.sencha.com/#fiddle/694

It is important to understand the difference between components and elements here. 在此处了解组件和元素之间的区别很重要。

A component is an instance of Ext.Component or one of its subclasses. 组件是Ext.Component或其子类之一的实例。 An element refers to the DOM. 元素是指DOM。

In your case, the renderTo configuration expects an element, but you're passing a component to it. 在您的情况下, renderTo配置需要一个元素,但是您正在向其传递组件。

When nesting components you will typically use containers. 嵌套组件时,通常将使用容器。

As Ext.Panel is a container all you need to do is add your button to it. 因为Ext.Panel是一个容器,所以您只需要将按钮添加到其中即可。 You can either do that by using the items config (as already pointed out in your question's comments): 您可以使用items config来做到这一点(正如您的问题注释中已经指出的那样):

var p = Ext.create('Ext.Panel',{
    renderTo:Ext.getBody(),
    title:'myPanel',
    items: [
        Ext.create('Ext.Button', {
            text: 'Click me!!!!',
            handler: function() {
                alert('You clicked the button!');
            }
        })
    ]
});

or - if you want to add the button after the component was already created - using the methods add or insert : 或-如果要在组件创建添加按钮-使用方法addinsert

p.add(Ext.create('Ext.Button', {
    text: 'Click me!!!!',
    handler: function() {
        alert('You clicked the button!');
    }
}));

Useful documentation resources: 有用的文档资源:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM