How to capture the URL parameters and based in url params redirect to other component in angular4

Issue

I have two applications.
From one applications we are coming to second applications.
from the first application

http://localhost:8080/myfirst-portal/?account_number=RDQsssyMDE=&firstname=UssssGhpbA==&lastname=RGFsssuY2U=&role=csssG9ydGFsZmllbGR1c2Vy

while coming from first application to second application it will send some data to second application.

second application
http://localhost:8080/myapp/#/login

Based on URL prams from the first application we have to redirect to specific component.

routes in angular app

{ path: ”, pathMatch: ‘full’, redirectTo: ‘/login’ },
{ path: ‘**’, pathMatch: ‘full’, redirectTo: ‘/login’ },

what is best way to achieve this?

Solution

subscribe to route events and then

ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
       this.id = +params['id']; // (+) converts string 'id' to a number

       // apply you condition here{
         this.router.navigateByUrl(['show_alunos']);
        }
    });
  }

and if you want to read detailed explanation you may read this topic
using click here having good topics

also, configure you route file

RouterModule.forRoot([
  { path: 'login', component: LoginComponent },
  { path: 'nomination', component: HomeComponent }
])

try with NavigationEnd

previousUrl: string;
constructor(router: Router) {
  router.events
  .filter(event => event instanceof NavigationEnd)
  .subscribe(e => {
    console.log('prev:', this.previousUrl);
    this.previousUrl = e.url;
  });
}

Answered By – Santosh Singh

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.