Why Embed Components
Large SAPUI5 apps turn into one giant view/controller hairball unless you split them. Reusable components — sap.ui.core.ComponentContainer plus a UIComponent — give you real boundaries: separate views, controllers, models, and routing, all loaded on demand.
Think of a dashboard that embeds the same "order list" widget in three places. Copy-paste the view three times and you now maintain three copies. Wrap it as a component and you maintain one.
The Minimal Reusable Component
A component needs a Component.js declaring its metadata and a root view. The host app drops a ComponentContainer wherever the widget should appear.
// Component.js
sap.ui.define([
"sap/ui/core/UIComponent"
], function (UIComponent) {
"use strict";
return UIComponent.extend("my.widgets.OrderList.Component", {
metadata: {
manifest: "json"
}
});
});
<!-- in the host view -->
<core:ComponentContainer
xmlns:core="sap.ui.core"
name="my.widgets.OrderList"
async="true"/>
With async="true" the component loads in parallel instead of blocking the host view's rendering. Always set it — synchronous component loading is a classic startup-time killer.
A component is a mini-app: own manifest, own views, own lifecycle. The host only knows its name and the container it sits in.
Passing Data In and Events Out
A reusable component must not reach into the host's models directly. The clean contract is: properties in, events out. Define the properties in the manifest under sap.ui5/componentData handling, or simpler — accept them through the container's componentData.
// host controller
this.byId("orderWidget").setComponentData({
customerId: "C1001",
compact: true
});
// Component.js init
init: function () {
UIComponent.prototype.init.apply(this, arguments);
var oData = this.getComponentData();
this.setModel(new JSONModel(oData), "cfg");
}
For events going the other way, the component fires events on its own event bus or uses the global sap.ui.getCore().getEventBus(). Say the widget needs to tell the host "user picked order 42" — publish an event, and every host subscribes without tight coupling.
Routing Inside Components
Each component can own a router with its own routes, declared in its manifest. The host's router and the component's router coexist — navigation targets stay scoped to their component. This is how you build shell apps where each tile area is an independently developed component with deep links.
Keep routers scoped per component. Cross-component navigation should go through events, not direct route calls — the host decides where "next" goes.
Gotchas Worth Knowing
- Model inheritance: unnamed models propagate down into components. Set named models explicitly inside the component to avoid surprises.
- ID collisions: views inside components get the component ID as prefix, so duplicates across instances are safe.
- Styling: component CSS lives in the component folder and loads with it — no global stylesheet pollution.
- Testing: components can be tested standalone by bootstrapping them directly, which makes unit tests far easier than testing a whole app.
Final Take
Component reuse is how you scale a UI5 codebase past one team. Define the contract (componentData in, events out), load async, and keep routers scoped — and your "shared widget" stops being a maintenance headache.
Have you shipped a reusable component that other teams actually adopted? Tell me what made the contract work in the comments.