-1

When I try to add a parameter to the constructor of a class I have an error: "Can't resolve all paremeters for Test c /: Users /....../ test.component.ts :(?).

import {Component} from "@angular/core";
@Component({
    selector:"app-rueba",
    template: "hola"
})
export class Prueba
{
    n:string;
    constructor(nombre:string)
    {
      this.n=nombre;
    }
}

Why is this happening?? Excuse me if the answer is pretty obvious I'm pretty new to angular

  • Does this answer your question? [EXCEPTION: Can't resolve all parameters](https://stackoverflow.com/questions/37997824/exception-cant-resolve-all-parameters) – R. Richards Jun 11 '20 at 15:46

1 Answers1

1

I do not see any need of having parameters in the constructor. You are not creating objects from the component like:

const object = new MyComponent(param);

In Angular, the constructor is used for injecting dependencies. The parameters are dependency injections. There is a difference between class and component. Read more here: https://angular.io/guide/dependency-injection

Maihan Nijat
  • 9,054
  • 11
  • 62
  • 110