使用新聚合的扩展控制导致错误 "Cannot add direct child without default aggregation defined for control"

Extended control with new aggregation causes error "Cannot add direct child without default aggregation defined for control"

我必须为 IconTabBar 做一个扩展,这样它就会在带有选项卡的行的右侧有一个操作按钮。所以我添加了一个文件到扩展 sap.m.IconTabBar 的项目,代码如下:

sap.ui.define([
  "sap/m/IconTabBar",
  "sap/m/IconTabBarRenderer"
], function (IconTabBar, IconTabBarRenderer) {
  "use strict";

  return IconTabBar.extend("nmsp.controls.IconTabBar", {
    renderer: IconTabBarRenderer,
    aggregations: {
      button: {
        type: "sap.m.Button",
        multiple: false
      }
    }
  });
});

我已将其添加到我的视图中,如下所示:

<cust:IconTabBar xmlns:cust="nmsp.controls">
  <cust:items>
    <!-- ... -->
  </cust:items>
  <cust:content>
    <!-- ... -->
  </cust:content>
<cust:IconTabBar >

到目前为止一切正常,但是当我尝试添加新聚合时:

<cust:IconTabBar xmlns:cust="nmsp.controls">
  <cust:items>
    <!-- ... -->
  </cust:items>
  <cust:button>
    <Button text="Upload new" />
  </cust:button>
  <cust:content>
    <!-- ... -->
  </cust:content>
<cust:IconTabBar>

页面加载失败并出现三重错误“无法添加没有为控件定义的默认聚合的直接子项...”。

IconTabBar 甚至没有默认聚合。我知道,命名空间没问题,因为当我在没有新聚合的情况下加载应用程序时,UI5 检查器显示呈现的控件是 nmsp.IconTabBar.

我知道如果不更改渲染器方法它是不可见的,但为什么它会破坏一切?

您的自定义控件缺少 aggregations 所属的 metadata 对象。所以应该是:

return IconTabBar.extend("nmsp.controls.IconTabBar", {
  <strong>metadata: {</strong> // <-- 向元数据添加聚合、属性等
    聚合:{
      // ...
    }
  <strong>}</strong>,
  // ...
});