我基於一個條件創建了Slides
。當條件成立時,Slides
組件被初始化。Angular,Ionic2每次初始化時都配置@ViewChild
<ion-slides *ngIf="condition === true">
<ion-slide *ngFor="let question of questions">
// slide content
</ion-slide>
</ion-slides>
Addionally我通過設置onlyExternal = true
禁用刷卡。由於這不是輸入屬性,我必須在父組件的ngAfterViewInit
方法中設置值。
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { Slides } from 'ionic-angular';
@Component{
...
}
export class QuizPage implements AfterViewInit {
@ViewChild(Slides) slides: Slides;
...
ngAfterViewInit {
this.slides.onlyExternal = true;
}
...
}
當視圖狀態更改Slides
-component被破壞,從而失去了onlyExternal
設置。當條件滿足時它會被重新初始化,但ngAfterViewInit
僅在父組件初始化時調用一次。這意味着onlyExternal
設置未設置。
目前我使用ngAfterViewChecked
,但它經常被調用,我想知道是否有更好的方法來解決這個問題。
ngAfterViewChecked() {
if(this.slides) {
this.slides.onlyExternal = true;
}
}
如何檢查條件? –
@ GabrielBarreto你是什麼意思,爲什麼它重要? –