Reputation: 257
I have add the provider for activedroute and now it give this error, i want to get query string parameter, for example : http://localhost:4200/?user=hello
I want get value of user, this is my code :
import { Component, OnInit, Input } from '@angular/core';
import {Router, ActivatedRoute, Params} from '@angular/router';
@Component({
selector: 'app-login',
templateUrl: './login.html'
})
export class LoginBancaComponent implements OnInit {
constructor(private route: ActivatedRoute){}
ngOnInit() {
this.activatedRoute.params.subscribe((params: Params) => {
let str= params['user'];
console.log(str);
});
}
}
But on console page there is this error:
Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?).
I have import everything in module :
import {Router, ActivatedRoute, Params} from '@angular/router';
...
imports: [
BrowserModule,
CommonModule, RouterModule,
],
providers: [ActivatedRoute],
...
Why there is this error ?
Upvotes: 1
Views: 8964
Reputation: 159
// 1. inject activatedRoute with name 'route'
constructor(private route: ActivatedRoute){ }
ngOnInit() {
// 2. use right name 'route' as injected above
this.route.params.subscribe((params: Params) => {
let str = params['user'];
console.log(str);
});
}
Upvotes: 1
Reputation: 8859
You don't need to provide ActivatedRoute
yourself, it is already provided in the RouterModule
. You get this error, because ActivatedRoute
has probably other dependencies which you do not provide. Just remove it from your providers
array in your module.
Upvotes: 7