Without touching the Component.js
content, if you can control both the initial HTML document (here: index.html
) and the JS file assigned to data-sap-ui-oninit
(here: myBootstrap.js
) you can attach a handler to the ComponentContainer
's componentCreated
event like this:
In index.html
:
<!-- ... -->
<head ...>
<!-- ... -->
<script id="sap-ui-bootstrap"
src="<...>resources/sap-ui-core.js"
data-sap-ui-async="true"
data-sap-ui-resourceRoots='{ "my.demo": "./" }'
data-sap-ui-oninit="module:my/demo/myBootstrap"
data-sap-ui-...="..."
></script>
</head>
<body id="content" class="sapUiBody">
<div data-sap-ui-component
data-id="myRootComponentContainer"
data-name="my.demo"
data-component-created="onMyComponentCreated"
data-height="100%"
data-settings='{ "id": "myRootComponent" }'
data-...="..."
></div>
</body>
</html>
In myBootstrap.js
:
// ...
globalThis.onMyComponentCreated = function(event) {
const myCreatedComponent = event.getParameter("component");
// ...
};
sap.ui.require(["sap/ui/core/ComponentSupport"]);
Here, the handler name "onMyComponentCreated
" is just a sample name. Make sure to create a name that would not cause a name collision in the global scope.
Regarding the data-component-created
syntax:
As HTML is case-insensitive, in order to define a property with upper-case characters, you have to "escape" them with a dash character. (From the API reference: sap/ui/core/ComponentSupport
)
For more information, review the documentation topic "Declarative API for Initial Components".