我的应用程序中有一些代码应该只在我的页面上执行一次,但似乎我所有的页面生命周期和构造函数都被调用了多次(每 x 秒调用一次(。例如,我的主页上没有同样的问题。有什么想法吗?组件和模块如下所示:
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ChamadaAtendimento } from './chamada-atendimento';
@NgModule({
declarations: [
ChamadaAtendimento,
],
imports: [
IonicPageModule.forChild(ChamadaAtendimento),
],
exports: [
ChamadaAtendimento
]
})
export class ChamadaAtendimentoModule {}
这是组件:
import { Component, OnInit } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { NavParams, NavController, ModalController } from 'ionic-angular';
import { ApiService } from '../../providers/api-service';
import { DialogService } from '../../providers/dialog-service';
import { AuthService } from '../../providers/auth-service';
import { HomePage } from '../home/home';
import { SetMotivoIntervalo } from '../set-motivo-intervalo/set-motivo-intervalo';
import { Platform } from "ionic-angular";
import { NativeAudio } from '@ionic-native/native-audio';
@IonicPage()
@Component({
selector: 'page-chamada-atendimento',
templateUrl: 'chamada-atendimento.html',
})
export class ChamadaAtendimento {
chamada_id : number;
area : string = "";
atividade : string = "";
resposta_enviada : boolean = false;
constructor(
navParams: NavParams,
private nav: NavController,
private api: ApiService,
private dialog: DialogService,
private auth: AuthService,
private platform: Platform,
private nativeAudio: NativeAudio,
private modalCtrl: ModalController
) {
this.chamada_id = navParams.data.chamada_id;
this.area = navParams.data.area;
this.atividade = navParams.data.atividade;
if (this.platform.is('cordova')) {
this.nativeAudio.preloadSimple('chamada', 'assets/sounds/Umbriel.mp3');
this.nativeAudio.loop('chamada');
}
}
ionViewWillEnter(){
console.log("page lifecycle test");
}
responderChamada(resposta){
console.log("resposta ",resposta),
console.log("resp enviada",this.resposta_enviada);
if(!this.resposta_enviada){
this.resposta_enviada = true;
if (this.platform.is('cordova')) {
this.nativeAudio.stop('chamada');
this.nativeAudio.unload('chamada');
}
if(resposta=="Intervalo"){
const modal = this.modalCtrl.create(SetMotivoIntervalo, {chamada_id: this.chamada_id});
modal.present();
}else{
this.dialog.showLoading("Enviando resposta...");
this.api.responder_chamada(this.chamada_id, resposta).subscribe(
() => {
this.dialog.loading.dismiss();
this.auth.setStatusAtual(resposta);
if (resposta=="Atendimento"){
this.auth.setChamada(this.chamada_id, this.area, this.atividade);
}else{
this.nav.setRoot(HomePage);
}
if(resposta=="Inativo"){
this.dialog.showToast("Status de Operação definido como Inativo");
}else{
this.dialog.showToast("Resposta enviada com sucesso");
}
},(error) =>{
this.dialog.loading.dismiss();
if(error && error.length){
this.dialog.showAlert("Erro ao responder chamada", error);
}
}
);
}
}
}
encerrarAtendimento(){
this.dialog.showLoading("Encerrando atendimento...");
this.api.encerrar_atendimento(this.chamada_id).subscribe(
() => {
this.dialog.loading.dismiss();
this.auth.setChamada(null, null, null);
this.nav.setRoot(HomePage);
this.dialog.showToast("Atendimento encerrado com sucesso");
},(error) => {
this.dialog.loading.dismiss();
if(error && error.length){
this.dialog.showAlert("Erro ao encerrar atendimento", error);
}
}
);
}
}
得到我的答案: 我的app.component
上有一个setInterval
,它每x秒调用一个函数。每次调用该函数时,它似乎都会交替组件,因此它必须重新构造,或者类似的东西,不确定。无论如何,对我来说的解决方案是为那一页clearInterval
。但仍然不太明白发生了什么
我沿着组件的路线,注意到缺乏验证。我把这个,它解决了问题。
Both codes:
redirect = () => this.navCtrl.setRoot(HomePage);
Old code:
this.redirect();
New code:
if (!this.redirected) {
this.redirected = true;
this.redirect();
}