угловой, не может прочитать свойство 'fruits' из undefined

<html>
    <head>
        <title>HTML Document</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    </head>

    <body>
        <div id="hover-id">
            Hello World
        </div>

        <script>
            jQuery(document).ready(function($){
                $(document).on('mouseover', '#hover-id', function(){
                    $(this).css('color','yellowgreen');
                });

                $(document).on('mouseout', '#hover-id', function(){
                    $(this).css('color','black');
                });
            });
        </script>
    </body>
</html>
-3
задан Jonathan 28 March 2019 в 01:18
поделиться

2 ответа

Вы должны инициализировать фрукты

export class myClass implements OnInit {

    public myArray: string[] = ['banana', 'Apple, 'Kumquat']
    public fruits: {test: boolean}[] = [];

    constructor() {}

    ngOnInit() {}

    public doSomething(): void {

      let my: any = {}; 

      this.myArray.forEach( m => {
        my = { test: true };
        this.fruits.push(my);
      });
    }
}
0
ответ дан George C. 28 March 2019 в 01:18
поделиться

Если вы хотите получить доступ к этому, вы должны использовать функции стрелок:

fruits: any[] = [];

doSomething() {
  this.myArray.forEach(m => {
    const my = { test: true };
    this.fruits.push(my);
  }
}

Функции стрелок приведут к его лексической ограниченности, что позволит вам получить доступ к this из самого контекста объекта ( внешняя сфера). Подробнее об этом здесь .

0
ответ дан wentjun 28 March 2019 в 01:18
поделиться
Другие вопросы по тегам:

Похожие вопросы: