2

I have an angular app with a single page for a confirmation that accepts a hash value in the query. There are no routes in the application. Just a single page and the component. How can I retrieve the hash value from the url query parameter?

I tried adding ActivatedRoute to the component but got an error saying there was no provider.

I would like to avoid having to add routes for this purpose, if possible.

I ended up using https://stackoverflow.com/a/31057221/10609045

workvact
  • 613
  • 2
  • 8
  • 14
  • 2
    if you're talking about the hash - `window.document.location.hash`. if you're talking about the query params - `window.document.location.search` – Stavm Nov 05 '18 at 17:34
  • Sorry, the hash value I meant was to retrieve the value of confirmid from something like http://example.com/confirm.html?confirmid=xYndm8ls1dkt – workvact Nov 05 '18 at 17:40
  • please provide some code on how to tried to add ActivatedRoute. It should be a dependency injection in constructor. – Sachin Gupta Nov 05 '18 at 17:41
  • The code is exactly like the one in conpile's answer. – workvact Nov 05 '18 at 17:51
  • @workvact can u share your code? without code it's hard to identify what's wrong in your code – Jameel Moideen Nov 05 '18 at 19:04
  • I ended up using Stavm's suggestion to use window.document.location.search and parse it. – workvact Nov 05 '18 at 19:24

2 Answers2

1

do i get it right, that you want to use ActivatedRoute in a component like this?:

@Component({..})
export class YourComponent {
   constructor(
           private readonly route: ActivatedRoute,
   ) {
      route.queryParams.subscribe( (params) => this.anyAction(params));
   }
}

have you added the RouterModule to the module where you declared your component?:

@NgModule({
    imports: [
        RouterModule
    ],
    declarations: [
        YourComponent
    ]
})
export class YourModule {}
Jameel Moideen
  • 7,542
  • 12
  • 51
  • 79
conpile
  • 66
  • 2
0

Check the answer at Angular error: no provider for ActivatedRoute

You need to import RouterModule.forRoot([]) in your AppModule to be able to use ActivatedRoute

Sachin Gupta
  • 4,981
  • 3
  • 16
  • 32
  • 1
    I have no routes defined and trying to use ActivatedRoute seems odd according to that answer. I just parsed window.document.location.search. – workvact Nov 05 '18 at 19:27
  • Good thought. No need to complicate things, just follow the KISS principle. – Sachin Gupta Nov 05 '18 at 19:32