1

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 ?

Panos Kalatzantonakis
  • 12,525
  • 8
  • 64
  • 85
Dell
  • 237
  • 2
  • 6
  • 13
  • what is the exact error could you print here – AddWeb Solution Pvt Ltd Dec 20 '17 at 11:35
  • 1
    Possible duplicate of [Error: (SystemJS) Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?)](https://stackoverflow.com/questions/40509325/error-systemjs-cant-resolve-all-parameters-for-activatedroute) – Robby Cornelissen Dec 20 '17 at 11:36
  • Uncaught Error: Can't resolve all parameters for ActivatedRoute: (?, ?, ?, ?, ?, ?, ?, ?). at syntaxError (vendor.bundle.js:21312) at CompileMetadataResolver.webpackJsonp.../../../compiler/@angular/compiler.es5.js.CompileMetadataResolver._getDependenciesMetadata (vendor.bundle.js:35399) – Dell Dec 20 '17 at 11:36
  • Robby Cornelissen i have just read that question but I don't know how you use RouterModule.forRoot() – Dell Dec 20 '17 at 11:38

2 Answers2

7

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.

Bunyamin Coskuner
  • 8,719
  • 1
  • 28
  • 48
1
// 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);
    });
}
kin
  • 139
  • 2
  • 6