docs

Enabling Routing in Nested Components

Every OpenUI5 component can define routing configuration in its manifest and a UI5 router instance will be created automatically after the component is instantiated.

Using components as targets in routing presents another challenge: When multiple components with their own routing configuration are used in an application, their router instances listen to the browser’s hashChange event simultaneously and may do concurrent changes to the hash. This can lead to conflicts, hence, the hash access has to be coordinated. Therefore, some additional configuration has to be made for these nested components to ensure everything is running stable.

Note:

Using routing with nested components requires asynchronous routing in all used components. The routers of all components have to enable asynchronous routing in their configuration. For more information, see Routing Configuration.


Configure a Component as Routing Target

A target in OpenUI5 routing can load either a view, or a component. To load a component, you need to define the component in the componentUsages section of the owner component’s manifest.json, see Using and Nesting Components.

Note:

If the configuration for a target can only be added at runtime, OpenUI5 provides APIs to add a target dynamically at runtime. For more information, see Navigate with Dynamic Targets.

Loading a child component with a type Component target in a router builds up a hierarchy between this router and the router in the child component.

{
    "sap.ui5": {
        "componentUsages": {
            "myreuse": {
                "name": "reuse.component",
                "settings": {},
                "componentData": {},
                "lazy": false
            }
        }
    }
}

Use the following configuration to load the component from the target:

{
    "sap.ui5": {
        "componentUsages": {
            "myreuse": {
                "name": "reuse.component",
                "settings": {},
                "componentData": {},
                "lazy": false
            }

        },
        "routing": {
            "config": {
                ...
            },
            "routes": [
                ...
            ],
            "targets": {
                "attachment": {
                    "type": "Component",
                    "usage": "myreuse",

                    "options": {
                        // optional
                        // define the additional parameter for
                        // instatiating the component instance
                    },
                    "containerOptions": {
                        // optional
                        // define the additional parameter for
                        // instantiating the component container
                        // which enables the component to be rendered
                        // in the parent control
                    },
                    "controlId": "page",
                    "controlAggregation": "content"
                }
            }
        }
    }
}

Configure Hash Prefix for the Nested Component

The hash from every router needs to be persisted in the browser hash. To identify the ownership of the hash segments from the browser hash, a prefix needs to be assigned to the component which is loaded by a Target. The prefix can be defined in the Route where the Target is used.

Instead of assigning the target option in a route with the name of a target which is going to be displayed once the route’s pattern is matched, an object is assigned which also contains the prefix of the hash for this component besides the name of the target. The loaded component from the target has its own hash segment which begins with the given prefix and can change the hash by using method navTo on Router in the same way as it is done in the top level component.

{
    "sap.ui5": {
        "componentUsages": {
            "myreuse": {
                "name": "reuse.component",
                "settings": {},
                "componentData": {},
                "lazy": false
            }
        },
        "routing": {
            "config": {
                ...
            },
            "routes": [{
                "name": "home",
                "pattern": "",
                "target": {
                    "name": "attachment",
                    "prefix": "atch"

                }
            }],
            "targets": {
                "attachment": {
                    "type": "Component",
                    "usage": "myreuse",
                    "options": {
                        // optional
                        // define the additional parameter for
                        // instatiating the component instance
                    },
                    "containerOptions": {
                        // optional
                        // define the additional parameter for
                        // instantiating the component container
                        // which enables the component to be rendered
                        // in the parent control
                    },
                    "controlId": "page",
                    "controlAggregation": "content"
                }
        }
    }
}

Propagate titleChanged Event from the Nested Component to the Parent Component

When the nested component myreuse has routing enabled, the router instance within the myreuse component fires on its own a titleChanged event once the displayed target has the title property defined. It is easier for an application to react to a titleChanged event if any titleChanged event(s) fired in the nested component(s) can be propagated to the router in the root component. To enable this, the property propagateTitle can be set in two ways:

If propagateTitle is not set, no titleChanged event will be propagated from the nested component.

{
    "sap.ui5": {
        "componentUsages": {
            "myreuse": {
                "name": "reuse.component",
                "settings": {},
                "componentData": {},
                "lazy": false
            }
        },
        "routing": {
            "config": {
                ...
            },
            "routes": [{
                "name": "home",
                "pattern": "",
                "target": {
                    "name": "attachment",
                    "prefix": "atch",

            "propagateTitle": true     
                }
            }],
            "targets": {
                "attachment": {
                    "type": "Component",
                    "usage": "myreuse",
                    "options": {
                        // optional
                        // define the additional parameter for
                        // instatiating the component instance
                    },
                    "containerOptions": {
                        // optional
                        // define the additional parameter for
                        // instantiating the component container
                        // which enables the component to be rendered
                        // in the parent control
                    },
                    "controlId": "page",
                    "controlAggregation": "content"
                }
        }
    }
}

The existing titleChanged event is extended with the following properties:

Related Information

Sample application: Routing with nested components