-1

In my angular application, i pass an object todo down to a child component via Input, which contains an array of strings at todo.tags. I now want to modify this array and therefore snap a copy of it and assign to a new variable like this:

@Input() todo: ITodos
public tags: Array<string> = [...this.todo.tags]

But i get the Error "property is used before initialization" which makes sense because i only declare todo . But i dont know how or for what i should initialize an Input, because the value is the input

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
WorldW1de
  • 71
  • 4

1 Answers1

0

The simple way is to init tags in getter for input like in this answer.

In case you are using RxJs in your project - you can pass an Observable, subscribe on it in the OnInit method and update tags on subscription firing.

@Input() todo: Observable<ITodos> = null;
public tags: Array<string> = null;

this.todo.pipe(untilDestroyed(this))
         .subscribe(value => this.tags = [...value.tags]);

P.s. @ngneat/until-destroy is used for subscription clean up in the example