extjs - 为什么我们需要 extjs 中的 childEls - 在哪里使用它?

标签 extjs

你能解释一下我必须使用这个吗?我有一个这样的例子,但我认为我们为什么要在 h1 标签中为它设置 .css ID 或 CLASS?为什么我们需要这个 childEls?当我对 msg 使用相同的技术时,为什么不起作用?

1) 我们为什么要使用这个配置? 2)在哪里使用? 3) 我们不能定义 .css class 或 id 和样式吗? 4) 当我对 msg 使用同样的技术时,它不起作用。

Ext.onReady(function () {
// Explicitly create a Container
Ext.create('Ext.Component', {
    renderTo: Ext.getBody(),
    renderTpl: [
        '<h1 id="{id}-title" data-ref="title">{title}</h1>',
        '<p>{msg}</p>',
    ],
    renderData: {
        title: "Error",
        msg: "Something went wrong"
    },
    childEls: ["title"],
    listeners: {
        afterrender: function(cmp){
            console.log(cmp);
            // After rendering the component will have a title property
            cmp.title.setStyle({color: "red"});
        }
    }
});

});

对于 mes,我使用了这段代码。

Ext.onReady(function () {
// Explicitly create a Container
Ext.create('Ext.Component', {
    renderTo: Ext.getBody(),
    renderTpl: [
        '<h1 id="{id}-title" data-ref="title">{title}</h1>',
        '<p id="{id}-msg" data-ref="msg">{msg}</p>',
    ],
    renderData: {
        title: "Error",
        msg: "Something went wrong"
    },
    childEls: ["title","msg"],
    listeners: {
        afterrender: function(cmp){
            console.log(cmp);
            // After rendering the component will have a title property
            cmp.title.setStyle({color: "red"});
            cmp.msg.setStyle({color: "green"});
        }
    }
});

});

谢谢!

最佳答案

当您使用 childEls 配置时,组件的构造函数将在组件内创建对 childEls 项的引用。

假设您的主要组件的 ID 为 component-2020,您的模板将创建

<h1 id="component-2020-title" data-ref="title">content of your renderData.title</h1>
<p id="component-2020-msg" data-ref="msg">content of your renderData.title</p>

现在因为你的 childEls 配置,每次你调用 component.title 或 component.msg 您将获得对这些特定元素的引用,并将能够对它们使用所有 Ext.dom.Elements 方法(此处描述:http://docs.sencha.com/extjs/5.0/apidocs/#!/api/Ext.dom.Element)。

所以它比仅仅替换 CSS 有用得多

您可以将您的 afterrender 方法更改为如下所示:

listeners: {
    afterrender: function(cmp){
        console.log(cmp);
        // After rendering the component will have a title property
        cmp.title.setStyle({color: "red"});
        cmp.title.fadeOut({duration: 2000}).fadeIn({duration: 2000});
        cmp.msg.setStyle({color: "green"});
    }
}
  • 您的消息 block 应该可以正常工作。我不知道为什么它对你不起作用。

关于extjs - 为什么我们需要 extjs 中的 childEls - 在哪里使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26403358/

相关文章:

Extjs 路由器传递多个参数

javascript - 如何扩展ExtJS SplitButton?

javascript - Extjs 4.没有显式 ID 的 location.hash

javascript - 在 ExtJS 中中止 Ajax 请求

session - 外部数据 session 和错误的服务器响应 ("[W] Ignoring server record: ...")

javascript - 过滤器未显示在网格中

javascript - 网格面板的 ExtJS Ajax 问题

javascript - 如何停止 setTimeout 循环?

javascript - 超时后重定向到其他 View

javascript - 如何在 ExtJs 的面板中搜索 child