Skip to Content

[UI5] How to Get Current Route Name?

Hi Experts,

In my application I got several routes like:

"routes": [
        {
          "pattern": "detail/Product",
          "name": "product",
          "target": "productTarget"
        },
        {
          "pattern": "detal/Account",
          "name": "account",
          "target": "accountTarget"
        }
]

For example: the user is now visiting the route ...../detail/Account, when he refresh the whole page with the fresh button in the browser, the page will load the account page again.

But in my App.controller.js's onInit function I would like to know what is the current route. For example when I refesh on product page, in App.controller.js's onInit function I could get current route is product, if I refresh on account page, in App.controller.js's onInit function I could get current route is account. Is this possible? I set "async: true" in my routing. It seems that onInit function of App.controller.js is always in front of checking current route?

Thank you!

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Feb 05 at 06:23 PM

    "sap/ui/core/routing/HashChanger"

    HashChanger.getHash()

    Add comment
    10|10000 characters needed characters exceeded

  • Feb 06 at 07:18 PM

    In order to get the current route name, make use of the event `routePatternMatched` which contains the route name in the event parameter.

    onInit: function() { // of root view
      this.getOwnerComponent().getRouter().attachRoutePatternMatched(function(event) {
        // event.getParameter("name") returns your current route name
      }.bind(this));
    },
    Add comment
    10|10000 characters needed characters exceeded

    • You're right! The root view cannot know any route name in onInit because the router is initialized after instantiating the root view. My bad. Thanks for pointing this out! I edited my answer accordingly.

  • Feb 06 at 06:29 AM

    You can try from the below.

    var url = window.location.href; // it fetches the whole URL.

    after that you decide the based on the index of the pattern, for example.

    if(url.indexOf("Product") > -1) // if it is true then the pattern is Product.

    Regards,

    Sagarika.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 07 at 01:17 PM
    Add comment
    10|10000 characters needed characters exceeded