TypeScript: Unable to read the "push" property from undefined in [null]

Error: Cannot read the "push" property from undefined in [null].

class A implements OnInit { stringArr: string[]; ngOnInit() { for(let i=0; i<4; i++) { this.stringArr.push("aaa"); } } } 
+5
source share
2 answers

You need to initialize the array:

stringArr = [];

+27
source
 class A implements OnInit { stringArr: string=[]; ngOnInit() { for(let i=0; i<4; i++) { this.stringArr.push("aaa"); } } } 

Here stringArr defined as a string, but not as an array of strings. Define stringArr as an empty array [].

0
source

Source: https://habr.com/ru/post/1244068/


All Articles