workvact
workvact

Reputation: 675

How to get query params in angular 6/7 without routes

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

Upvotes: 3

Views: 8819

Answers (2)

conpile
conpile

Reputation: 66

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 {}

Upvotes: 1

Sachin Gupta
Sachin Gupta

Reputation: 5311

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

Upvotes: 0

Related Questions