【问题标题】:TypeError: Cannot read properties of undefined (reading 'slideNext') (Ionic 5)TypeError:无法读取未定义的属性(正在读取'slideNext')(Ionic 5)
【发布时间】:2021-10-01 14:54:28
【问题描述】:

我在 Ionic 5 中创建了一个应用程序,其中包含多个使用幻灯片的页面。我创建了一个服务来存储所有全局函数,并从每个页面的任何 TS 文件中调用它们。

当我尝试调用页面中的功能(正在使用)时,除了包含对幻灯片(离子幻灯片)的引用的功能外,一切都很好。

如果您有任何想法,请帮助我吗?如果您想了解有关该问题的更多详细信息,请不要犹豫。

提前致谢

service.ts 文件

import { Injectable, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { Component } from '@angular/core';
import { NavController, NavParams, IonSlides } from '@ionic/angular';

@Injectable({
  providedIn: 'root'
})
export class HttpService {



  @ViewChild('slides') 
  slides:IonSlides;

  slideOptions = {
    initialSlide: 0,
    slidesPerView: 1,
    autoplay: false,
    pagination : {
      el: '.swiper-pagination',
      clickable: true
    },
    //allowSlidePrev:true,
    /*
    coverflowEffect: {
      rotate: 50,
      stretch: 0,
      depth: 100,
      modifier: 1,
    },*/
      slideShadows: true,
  };

  constructor(private router: Router){}

  
  async navigate(){
    this.router.navigate(['tabs/tab2'])
    await this.waitBefore(10);// Sleep thread for 10ms seconds (permet de réinitialiser radio states)
    this.router.navigate(['/quiz-maison'])
  }


  isOK:boolean=null;
  answers: any;
  hasAnswered: boolean = false;
  initState: boolean = false;
  score: number = 0;
  //allowSlidePrev=false;
  showAnswer(event:any) {
    // get data throught event emitter
      this.answers = event.target.value;
      if (this.answers=="correct"){
      //this.answers= this.bon;
      this.isOK=true;
      this.score++;

      }

      else{
      //this.answers= this.mauv;
      this.isOK=false;false
      }
      this.hasAnswered = true;
  console.log("I'm showAnswer fct");
  console.log(this.slides);
  this.slides.slideNext();
  }

  ionSlideChange(){
  this.answers =null;
  this.isOK=null;
  this.hasAnswered = false;
  }

  
waitBefore(ms: number)
{
  return new Promise(resolve => setTimeout(resolve, ms));
}

  async goNextSlide(){
    await this.waitBefore(1000);// Sleep thread for 3 seconds
    //this.slides.lockSwipeToPrev(false);
    this.slides.slideNext();
    this.slides.lockSwipeToPrev(true);
    console.log("I'm goNextSlide fct");
  }

}

maison.ts 文件:

import { Component, OnInit, ViewChild } from '@angular/core';
import { IonSlides, NavController, NavParams } from '@ionic/angular';
import { Router } from '@angular/router';
import { HttpService } from '../../service/http.service';

@Component({
  selector: 'app-quiz-maison',
  templateUrl: './quiz-maison.page.html',
  styleUrls: ['./quiz-maison.page.scss'],
  //providers: [NavParams]
})
export class QuizMaisonPage implements OnInit {

  @ViewChild('slides') slides: IonSlides;
    slideOptions = {
    initialSlide: 0,
    slidesPerView: 1,
    autoplay: false,
    pagination : {
      el: '.swiper-pagination',
      clickable: true
    },
    //allowSlidePrev:true,
    /*
    coverflowEffect: {
      rotate: 50,
      stretch: 0,
      depth: 100,
      modifier: 1,
    },*/
      slideShadows: true,
  };
  
  constructor(private http:HttpService){}

  ngOnInit() {
  }


  showAnswer(){
  this.http.showAnswer(event)
  }

  goNextSlide(){
    this.http.goNextSlide();
    }

    goToAnswers(){
      this.http.goToAnswers();
    }

  ionSlideChange(){
    this.http.ionSlideChange()

  }

  waitBefore(){
    let ms: number;
    this.http.waitBefore(ms)
  }
}

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    尝试传递对您的服务类的引用(使显示答案也将幻灯片作为输入参数),另外,我认为在您的服务中包含 @ViewChild 是不正常的。

    根据角度文档Property decorator that configures a view query. The change detector looks for the first element or the directive matching the selector in the view DOM. If the view DOM changes, and a new child matches the selector, the property is updated.

    就其本质而言,该服务与任何视图无关。因此,这就是它未定义的原因,因为它没有附加到它的视图。

    【讨论】:

    • 感谢您的回答和详细信息。但我很奇怪,为什么当我调用 CONSOLE.LOG 等其他函数(在服务中定义)并在页面中调用时,调用工作正常!为什么它与幻灯片组件 (IonSlides) 不同。我不得不提一下,我从 './service/http.service' 导入了 import { HttpService };在 app.module.ts 并添加它提供这样的列表: providers: [{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },AppVersion, HttpService]
    • 我问为什么我没有在 app.module.ts 的 IMPORT 列表中添加它,这是我的第一个想法,但我得到了错误:“没有 emod”
    • 那么是服务本身为空吗?
    • 老实说,我不知道,我被阻止了,我发现继续前进的唯一方法是在我的应用页面的每个 TS 文件中创建相同的函数。我知道这不是一个好习惯。
    【解决方案2】:

    确保您的 html 文件中有 ,因为这是您在 maison.ts 中为变量提供的名称

      <ion-slides pager="true" style="height: 100%;" #questionario (ionSlideWillChange)="slideChanged()"> 
    
     @ViewChild('questionario') slides: IonSlides;
    

    【讨论】:

    • 感谢您的回答,我做到了(通过在 HTML 中添加#slides 创建幻灯片的引用)但问题仍然存在
    猜你喜欢
    • 2020-08-05
    • 2019-01-27
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2022-08-08
    • 2022-07-22
    • 2018-10-29
    相关资源
    最近更新 更多