Dell
Dell

Reputation: 257

Angular 2 : Can't resolve all parameters for ActivatedRoute

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

Answers (2)

kin
kin

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

Bunyamin Coskuner
Bunyamin Coskuner

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

Related Questions