为什么我的组件不调用它的 init 方法?

Why is my Component not calling its init method?

我有一个 UIComponent,它依赖于一个匿名组件。

主要组件元数据:

jQuery.sap.declare("MYAPP.Component");

sap.ui.core.UIComponent.extend("MYAPP.Component", {
  metadata: {
    dependencies: {
      libs: [],
      components: [
        "MYAPP.Component2"
      ]
    }, etc

该应用程序指示它已到达匿名组件,因为如果我故意犯了一些语法错误,我会在加载网页时收到错误消息。我还可以获得 console.log("test") 从 sap.ui.core.Component.extend() 代码外部打印出来。

jQuery.sap.declare("Component2.Component");

console.log("outside test"); //this prints

sap.ui.core.Component.extend("Component2.Component", {
  metadata: {
  },

  init: function(){
    sap.ui.core.Component.prototype.init.apply(this, arguments);
    console.log("component2 init test"); //this doesn't print
  }

});

也许我的资源声明有问题?
index.html中的一些:

<script id='sap-ui-bootstrap' type='text/javascript'
    src='resources/sap-ui-core.js'
    data-sap-ui-theme='sap_bluecrystal'
    data-sap-ui-libs='sap.m, sap.me'
    data-sap-ui-xx-bindingSyntax="complex"
    data-sap-ui-xx-supportedLanguages="en"
    data-sap-ui-resourceroots='{"MYAPP":"./"}'></script>

<script>
  sap.ui.localResources("view");
  sap.ui.localResources("utils");
  sap.ui.localResources("control");
  sap.ui.localResources("Component2");

我的文件夹结构:

MYAPP
  /Component2               //faceless component folder
    Component.js
  /view                     //views and controllers folder
  Component.js              //main component
  index.html

我在 SCN (here) 的用户帮助下找到的答案是,父组件的元数据将调用一个函数来加载新组件而不实例化它。因此,您还必须显式创建组件。

我在父组件的 init() 函数中添加了一个简单的 sap.ui.component。

sap.ui.component({ name: "MYAPP.Component2" });

现在新组件在父组件之前被加载和实例化。希望这个 post 可以帮助其他人使用不露面的组件,因为我自己在研究这个问题时发现的文档很少。