I need to retrieve URL queries as in www.website.com?a:b
.
For this I followed this official angular tutorial, leading me to this simple code inside my component:
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.route.queryParams.subscribe(params => {
console.log(params)
})
}
This triggers the following error in the console:
ERROR NullInjectorError: R3InjectorError(AppModule)[ActivatedRoute -> ActivatedRoute -> ActivatedRoute]: NullInjectorError: No provider for ActivatedRoute!
Event though it is not mentioned in the tutorial, I add ActivatedRoute
to my app.module.ts
. This generates a different error:
Error: NG0204: Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?).
I cannot tell why noone else has had this issue, does anyone know what the problem might be? Thanks.
CodePudding user response:
I think what you are trying to achieve could look like the following:
this.sub = this.activatedRoute.params.subscribe((params : Params) => {
// You now have all params in a single variable 'params'
});
CodePudding user response:
Most likely in your app.module or main.module file you need to add an import. If you look at this link, which is at the bottom of the page you have linked, you will see
@NgModule({
imports: [
BrowserModule,
FormsModule,
// This is what provides the ActivatedRoute service
// You likely don't need useHash: true, so you can leave it out
RouterModule.forRoot(routes, { useHash: true }) // .../#/crisis-center/
],
declarations: [
AppComponent,
PageNotFoundComponent
],
providers: [
],
bootstrap: [ AppComponent ]
})
export class AppModule { }