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);
        }
      );
    });
  }
}
                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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With