This topic describes how to initialize routing in a component and access the routing functions.
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.
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:
var oRouter = this.getRouter();
All views that are generated by the router are automatically created in the context of the component. To access the router from a controller, use the getOwnerComponent()
function to get the owner component, which provides the getRouter()
function:
var oRouter = this.getOwnerComponent().getRouter();
Related Information