Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aurelia clear route history when switching to other app using setRoot

Tags:

aurelia

The Aurelia router remembers which page I was last on, even after using setRoot() and it will redirect me to that page, even though I would want to land on the main app page again.

I'll try to explain it in a usecase. I have two apps: login and app. I login in login app and get redirected to app. I navigate to /securedPage on app and then proceed to log out and get redirected to login again. I login with another user on login and then I get redirected to app/securedPage. I want to be and should be redirected to just app.

How do I clear the route history when switching between apps with setRoot()?

like image 941
Tarps Avatar asked Dec 25 '22 07:12

Tarps


1 Answers

Wanted to help out, recently got this working and the suggestions above almost work, but they are missing some parts. In this thread, the creator of Aurelia (Eisenberg) repsonds with a suggestion: https://github.com/aurelia/framework/issues/590

So to switch app root do the following:

    this.router.navigate('/', { replace: true, trigger: false });
    this.router.reset();
    this.router.deactivate();

    this.aurelia.setRoot('app');

In my case I could actually skip the reset and deactivate part and just do

    this.router.navigate('/', { replace: true, trigger: false });

However doing this.router.navigate('/') without the replace and trigger part caused problems especially when switching app root multiple times.

So make sure to add:

     ... { replace: true, trigger: false });
like image 142
Johan O Avatar answered Apr 17 '23 07:04

Johan O