3

I am using a hash location strategy in Angular

routing.module.ts

@NgModule({ 
imports: [
   RouterModule.forRoot(routes, { useHash: true })
 ],
 exports: [
    RouterModule
  ]
})

app.module.ts

@NgModule({
    providers: [{ provide: LocationStrategy, useClass: HashLocationStrategy }]
})

I am routing from some other application to my angular application with this URL

HTTP://localhost:4200?param_x=xyz&param_y=abc

I want to get values of these params in my application, how do I do that?

I tried using Activated route snapshot as well as subscribe, both give me empty values.

Amod Shinde
  • 144
  • 9

1 Answers1

2

Please open your client side with fragment params.

Fragment params are parameters after # - also called as client params because they arent sent to the server.

HTTP://localhost:4200?#hash_param_x=xyz&hash_param_y=abc

Then access them via:

this.activatedRoute.snapshot.queryParams

HashLocationStrategy takes the hash(fragment/client) params and insert them into query params. Since you don't have any, you don't see any params.

Raz Ronen
  • 2,418
  • 2
  • 14
  • 26
  • I have both scenarios, I am redirected to the angular application with my params from the server and I want to use those. apart from that, I use the params internally within the application. – Amod Shinde Aug 18 '20 at 06:44
  • So open your client side with fragment params and not query ones. Does this work for you? Once you open it one time, you shouldn't be bothered with anymore. – Raz Ronen Aug 18 '20 at 06:46
  • I updated my answer to reflect that. After inital redirect there's no need for extra work. You can use snapshot.queryParams all the way. – Raz Ronen Aug 18 '20 at 06:48
  • 1
    Worked perfectly for me! Thanks – Amod Shinde Aug 18 '20 at 06:58