Navigating to Routes with Hard-Coded Patterns

Objective

After completing this lesson, you will be able to navigate to a route that has a hard-coded pattern.

Hard-Coded Routing Patterns

Whenever a hash is added to a URL, the router checks whether there is a route with a matching pattern. SAPUI5 provides different types of routing patterns for configuring a route.

Routing code, highlighting the route named detail.

In the example shown, a route named detail is defined for which the target named detail is displayed, which in turn is used to load a view named Detail.

To define the route, the hard-coded pattern detail is used. This pattern only matches if the hash of the browser is detail and no data is passed on to the events of the route.

Method for Navigation

Use the router's navTo method to navigate to a specific route by setting the browser's hash accordingly.

To access the router instance from a view controller, you can use the controller's getOwnerComponent method, to get access to the owner component, which provides the getRouter method.

The navTo method has the following parameters:

  • sName - name of the route
  • oParameters (optional) - the parameters for the route
  • oComponentTargetInfo (optional) - information for route name and parameters of the router in nested components
  • bReplace (optional) - if set to true, the hash is replaced, and there will be no entry in the browser history. If set to false (default), the hash is set and the entry is stored in the browser history.

Accessing the Router Instance and Triggering the Navigation

Sample router code using the navTo method.

In the example shown, the navTo method of the router is used in an event handler method of a view controller to navigate to the route named detail. This sets the pattern detail of the detail route (see above) to the browser's hash. SAPUI5 then reacts to the browser's hashchange event to find the route that matches that hash. This results in the target of the detail route - in the end, the Detail view - being displayed in the browser.