Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing Strategies In Angular 2.0. Can i mantain my own history stack?

i have a peculiar situation in Angular 2.0 with routing

  1. I don't want to repeat a navigation. i.e if a user goes to page a -> then goes to page b-> then again goes to page a-> then goes to page c and when the user presses back button, the user should be redirected to page b and not page a. What i want to achieve is Pressing the back button multiple times should not display the same page more than once. So if i do a>b>c>a>c>b>a>c>b>a and then start pressing back : pages a,b,c should come only once & not based on regular browser behavior.

  2. On successful login, if i press back button, the login screen shouldn't come, rather it should exit or something like a blank URL and not the login URL.

Any suggestions how to implement this ?

Should i use NavigationStart/NavigationEnd & write my logic there ?

like image 226
Parth Ghiya Avatar asked Dec 18 '16 05:12

Parth Ghiya


1 Answers

I solved the following by writting my own LocationStrategy

@Injectable()
export class TestClass implements LocationStrategy {

path(includeHash?: boolean) : string{
       //write my own logic here 
    }
pushState(state: any, title: string, url: string, queryParams: string) : void{
         console.log("push state called");
        //write my own logic here
    }
}

And in my app.module.ts

  {provide:LocationStrategy,useClass:TestClass },

This way i was able to achieve my requirements.

like image 109
Parth Ghiya Avatar answered Oct 24 '22 07:10

Parth Ghiya