超过最大调用堆栈大小(Angular8)

时间:2020-02-23 22:32:16

标签: angular typescript

我正在尝试在Angular中执行以下操作,我不明白问题出在哪里? 了解这是一个递归函数,但是在javascript中,有很多解决方法可以解决此问题,我在这里尝试了一下,但没有解决 您对此有何解决方法?

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
  constructor() {}

  changeImg = () => {
    let i = 0;
    const time = 1000;
    const images = [
      '../../assets/imgs/dashboard_full_1.webp',
      '../../assets/imgs/dashboard_full_2.webp'
    ];
    document.slide.src = images[i];
    if (i < images.length - 1) {
      i++;
    } else {
      i = 0;
    }

    setTimeout(this.changeImg(), time);
  };

  ngOnInit() {
    this.changeImg();
  }
}

1 个答案:

答案 0 :(得分:0)

您的问题在这里:

Theme.of(context).copyWith(
    highlightColor: Colors.transparent,
    splashColor: Colors.transparent),
    child: PopupMenuItem(...),
);

您正在调用 setTimeout(this.changeImg(), time); ,该调用将调用this.changeImg(),然后调用this.changeImg(),并一直持续进行下去,直到Angular对您无聊并导致应用崩溃。 >

如果未发生调用堆栈大小错误,您应该将this.changeImg()的结果传递给this.changeImg(),而不是将 reference 传递给预期的函数

只需删除调用即可。

setTimeout