Show TOC

Initializing and Accessing a Routing InstanceLocate this document in the navigation structure

This topic describes how to initialize routing in a component and access the routing functions.

Initializing

You initialize the router in the component using the following code:

sap.ui.define([
    "sap/ui/core/UIComponent", ...
], function (UIComponent, ...) {
    "use strict";

    return UIComponent.extend("YourComponentClassName", {

        ...,

        init: function () {
            ...
            // call the init function of the parent
            UIComponent.prototype.init.apply(this, arguments);
            // this component should automatically initialize the router
            this.getRouter().initialize();
            ...
        }
    });
});

The router instance is automatically destroyed when the component is destroyed.

Accessing

To access the router and to use its functions, use the getRouter() function or the static getRouterFor function of the UI component. You can pass either a controller, or a view:

var oRouter = sap.ui.core.UIComponent.getRouterFor(this);

You can also use the getRouter function of your UI component.

All views that are generated by the router are automatically created in the context of the component.