Overview
You will learn
- How to create a Luigi view with UI5
- How to import Luigi Client to your view
Prerequisites
- You have completed Build Luigi App with React and have a running Luigi Core React application with the
ui5-mfproject set up.
Steps
Intro
In this tutorial, you will create a UI5 micro-frontend for an “Order History” view and connect it to the Luigi Core React application using the Luigi Client.
In this step, you will import Luigi Client to the UI5 micro-frontend so you can use linkManager and other API function in the next steps.
Open
ui5-mf/webapp/index.htmland add this line in the head section of the file:HTML<!--Inside the head tag --> <script src="https://unpkg.com/@luigi-project/client@2.33.0/luigi-client.js"></script>In the same file, change the bootstrap option from
data-sap-ui-frame-options="trusted"todata-sap-ui-frame-options="allow". Directly below, adddata-sap-ui-preload="". Note that the UI5 generator uses local resources (src="resources/sap-ui-core.js") and hyphenated bootstrap attributes, so your generated file should look like this afterwards:HTML<script id="sap-ui-bootstrap" src="resources/sap-ui-core.js" data-sap-ui-resource-roots='{ "luigi.ui5": "./" }' data-sap-ui-on-init="module:sap/ui/core/ComponentSupport" data-sap-ui-compat-version="edge" data-sap-ui-frame-options="allow" data-sap-ui-preload="" data-sap-ui-async="true" ></script>
In this step, you will add a new navigation node in Luigi config hosted in the React application, then create the micro-frontend content using UI5.
Add an “Order History” node to your Luigi navigation in
react-core-mf/public/luigi-config.js:JavaScriptchildren: [ { pathSegment: "products", label: "Products", icon: "product", viewUrl: "/sampleapp.html#/microfrontend/products", keepSelectedForChildren: true, children: [{ pathSegment: ':id', viewUrl: '/sampleapp.html#/microfrontend/productDetail/:id', context: { id: ':id' } }] }, // <--- Don't forget to add a comma here ---> //<---Add the section below to the Luigi config---> { pathSegment: 'order', label: 'Order History', icon: 'history', viewUrl: 'http://localhost:8080/index.html' } //<------> ],Open the
ui5-mf/webapp/view/Main.view.xmlfile in your UI5 app, and replace the content with:XML<mvc:View controllerName="luigi.ui5.controller.Main" displayBlock="true" xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc"> <List items="{/ProductCollection}"> <ObjectListItem title="{name}" type="Active" press="onListItemPress" number="{ parts:[{path:'price'},{path:'currencyCode'}], type: 'sap.ui.model.type.Currency', formatOptions: {showMeasure: false} }" numberUnit="{currencyCode}"> <ObjectAttribute text="Quantity: {orderQuantity}" /> </ObjectListItem> </List> </mvc:View>Open
ui5-mf/webapp/controller/Main.controller.jsand replace it with:JavaScriptsap.ui.define([ "luigi/ui5/controller/BaseController", "sap/ui/model/json/JSONModel" ], function (Controller, JSONModel) { "use strict"; return Controller.extend("luigi.ui5.controller.Main", { onInit: function () { const oModel = new JSONModel(); oModel.loadData(sap.ui.require.toUrl("luigi/ui5/model/products.json")); this.getView().setModel(oModel); }, onListItemPress: function (oEvent) { const id = oEvent.getSource().getBindingContext().getProperty("id"); LuigiClient.linkManager().openAsModal('/home/products/' + id, { title: 'Product Detail', size: 'm' }); } }); });Navigate to
ui5-mf/webapp/modeland create aproducts.jsonfile with the following content.
{
"ProductCollection": [
{
"id": 101,
"name": "Mouse",
"price": 45.0,
"stock": 80,
"icon": "product",
"currencyCode": "EUR",
"orderQuantity": 2,
"description": "Wireless Gaming Mouse with Sensor"
},
{
"id": 102,
"name": "Keyboard",
"price": 50.0,
"stock": 22,
"icon": "product",
"currencyCode": "EUR",
"orderQuantity": 1,
"description": "A physical keyboard that uses an individual spring and switch for each key. Today, only premium keyboards are built with key switches."
},
{
"id": 103,
"name": "Optical Mouse",
"price": 35.0,
"stock": 4,
"icon": "product",
"currencyCode": "EUR",
"orderQuantity": 2,
"description": "Utilizing the latest optical sensing technology, the USB Optical Scroll Mouse records precise motion."
},
{
"id": 104,
"name": "Laptop Pro",
"price": 1299.0,
"stock": 11,
"icon": "laptop",
"currencyCode": "EUR",
"orderQuantity": 3,
"description": "Newest laptop featuring a touch-sensitive OLED display."
},
{
"id": 105,
"name": "Mouse 2",
"price": 40.0,
"stock": 20,
"icon": "product",
"currencyCode": "EUR",
"orderQuantity": 6,
"description": "The Mouse 2 is a computer mouse featuring a multi-touch acrylic surface for scrolling. The mouse features a lithium-ion rechargeable battery and Lightning connector for charging and pairing."
},
{
"id": 106,
"name": "Printer",
"price": 235.0,
"stock": 24,
"icon": "fx",
"currencyCode": "EUR",
"orderQuantity": 1,
"description": "Affordable printer providing you with the optimal way to take care of all your printing needs."
},
{
"id": 107,
"name": "Phone 11",
"price": 835.0,
"stock": 45,
"icon": "iphone",
"currencyCode": "EUR",
"orderQuantity": 8,
"description": "The Phone 11 dimensions are 150.9mm x 75.7mm x 8.3mm (H x W x D). It weighs about 194 grams (6.84 ounces)."
},
{
"id": 108,
"name": "Phone 3a",
"price": 299.0,
"stock": 54,
"icon": "desktop-mobile",
"currencyCode": "EUR",
"orderQuantity": 7,
"description": "At 5.6 inches, the display is proportionate to the relatively small body of the phone."
},
{
"id": 109,
"name": "Game Console 4",
"price": 330.0,
"stock": 94,
"icon": "video",
"currencyCode": "EUR",
"orderQuantity": 1,
"description": "This is the fourth home video game console compatible with all gaming systems."
},
{
"id": 110,
"name": "Monitor",
"price": 630.0,
"stock": 20,
"icon": "sys-monitor",
"currencyCode": "EUR",
"orderQuantity": 3,
"description": "34'' Monitor, Display with stand Height adjustable (115 mm), tiltable (-5° to 21°), rotatable (-30° to 30°) Security slot (cable lock sold separately), anti-theft slot for locking to stand (for display). Includes: DisplayPort cable, HDMI cable, Power cable, Stand, USB 3.0 Type-A to Type-B cable, USB-C cable."
}
]
}You can run the UI5 project to check if it’s assembled correctly. Open a terminal/command prompt and navigate to ui5-mf, then execute:
npm startYou should be able to access the app at http://localhost:8080/index.html. If you want to see the UI5 project in the context of the larger app, open a new terminal window, navigate to react-core-mf and run npm start. Refresh your browser to see the changes.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.