Skip to Content
0

[UI5] How to Get Current Route Name?

Feb 05 at 03:21 PM

276

avatar image

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!

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Jun Wu Feb 05 at 06:23 PM
0

"sap/ui/core/routing/HashChanger"

HashChanger.getHash()

Show 4 Share
10 |10000 characters needed characters left characters exceeded

Thank you Jun, very helpful!

0

This is great. But the current hash value just indicates which route was navigated (which seems to be sufficient in this case). In order to get the actual current route name, a routing related event is needed.

0
Former Member

This will not work if you have attributes in your route. Hash != route name.

0
Former Member

he just wants to know where he is, not exactly the name....

0
Boghyon Hoffmann Feb 06 at 07:18 PM
1

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));
},
Show 2 Share
10 |10000 characters needed characters left characters exceeded

you 100% sure the following is getting called before view init?

function(event){
    this.setCurrentRouteName(event.getParameter("name"));}
1

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.

0
Sagarika Gattu Feb 06 at 06:29 AM
1

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thank you Sagarika, thank you for the answer. I chose Jun's answer but your approach also works.

0
avatar image
Former Member Feb 07 at 01:17 PM
0
Share
10 |10000 characters needed characters left characters exceeded