首页 > 解决方案 > 面对不是函数错误,但它在 Angular 上

问题描述

我正在开发一个 html、css、js 滑块,但我遇到了一个奇怪的错误。我正在研究角度

这是错误

    core.js:6479 ERROR TypeError: this.slideFunction is not a function
    at autoSlide (home.component.ts:32)
    at timer (zone.js:2561)
    at ZoneDelegate.invokeTask (zone.js:406)
    ......................................
    ......................................

这就是我的代码的样子

export class HomeComponent implements OnInit {
      counter:number = 1;
      timer: any;

      constructor() { }

      ngOnInit(): void {

        //change the current slide to the next one after 8 secondes by calling the autoSlide function
        this.timer = setInterval(this.autoSlide,8000);

      }


      autoSlide() {
       this.counter += 1;
       this.slideFunction(this.counter);
      }

      //Function that handle the change
      slideFunction(slideNumber: number): void{
   
        My logic goes there...

      }
      }

有人可以显示我在哪里做错了吗?谢谢

标签: angularfunctionsetinterval

解决方案


您正在调用slidefunction()之前定义的相同。

先写函数,再调用。


推荐阅读