Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A parameter property is only allowed in constructor implementation

I've written this code and this line use(private lang: string): Promise<object> is giving me an error: "A parameter property is only allowed in constructor implementation".

It works when I remove access modifier private but I'm just curious why it gives me this error and what is the right way?

@Injectable()
export class TranslateService {

  public data: object = {};

  constructor(private http: HttpClient) {}

  use(private lang: string): Promise<object> {
    return new Promise<object>((resolve, reject) => {
      const langPath = `assets/i18n/${lang || 'en'}.json`;

      this.http.get<object>(langPath).subscribe(
        translation => {
          this.data = Object.assign({}, translation || {});
          resolve(this.data);
        },
        error => {
          this.data = {};
          resolve(this.data);
        }
      );
    });
  }

}
like image 689
Mohit Bumb Avatar asked Feb 11 '19 09:02

Mohit Bumb


1 Answers

Remove private in

use(private lang: string): Promise<object> {

private and public are only used in the class level, not function level (they're always private).

The only function in a class accept public or/and private is the constructor function, as it will assign values and create properties for the class.

like image 162
Nguyen Phong Thien Avatar answered Nov 12 '22 17:11

Nguyen Phong Thien