5

I have a simple component:

export class PlaintextComponent implements OnInit {
  schema: PlaintextTagSchema;

  constructor(private _ngZone: NgZone, prompt: string, maxRows: number, maxChars: number) {
    this.schema.prompt = prompt;
    this.schema.maxRows = maxRows;
    this.schema.maxChars = maxChars;
  }

  ngOnInit() {
  }
}

When I try to compile my app using ng serve, I get the error:

component.ts:25:40 - error NG2003: No suitable injection token for parameter 'prompt' of class 'PlaintextComponent'.
Found string

25   constructor(private _ngZone: NgZone, prompt: string, maxRows: number, maxChars: number) {

I've looked all over on the internet for this cryptic error and haven't found anything resembling a fix. It looks like a correct constructor to me, but I'm new to angular/TS and it's highly possible i'm missing something incredibly basic. Thanks in advance for the help.

6
  • 1
    Try type any and cast to string inside the constructor Commented Mar 16, 2020 at 0:26
  • 1
    How and where are you passing prompt, maxRows and maxChars values to the PlaintextComponent? Commented Mar 16, 2020 at 0:26
  • 1
    Does this answer your question? Angular 7 - build --prod failed with error: Can't resolve all parameters for Commented Mar 16, 2020 at 4:09
  • Nilanka, this just changes the error to "found any" Commented Mar 18, 2020 at 1:12
  • bjdose, I cannot even create a plaintextcomponent yet, the definition doesn't compile Commented Mar 18, 2020 at 1:13

2 Answers 2

2

Rewrite the Constructor as following

  constructor(private _ngZone: NgZone, @Inject(String) prompt: string, maxRows: number, maxChars: number)

dont forget

import { Inject } from '@angular/core';
Sign up to request clarification or add additional context in comments.

1 Comment

This should be accepted as the answer
1

I had the same error. Looks like Angular is trying to do some dependecies injection with the parameters in the constructor (like the ones you do with services), but in your case, this will lead to errors. Just declare your class properties out of the parameters of the constructor. Move your class properties out of the constructor like this:

export class PlaintextComponent implements OnInit {
  schema: PlaintextTagSchema;
  private _ngZone: NgZone;
  prompt: string;
  maxRows: number; 
  maxChars: number;

  constructor() {}

  ngOnInit() {
  }
}

You maybe have to find another way to initialize your Component, like input properties.

Please, let me know if it works! ;D

1 Comment

This helped me find the solution - I didn't really understand how constructors in angular worked, and turns out what I was trying to do generally wasn't acceptable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.