2017-05-30 220 views
0

我不知道我在哪裏出錯了ionViewDidLeave。我收到來自終端的錯誤消息:「找不到名稱ionViewDidLeave」。有什麼我必須導入使用它?我已經導入了navController。錯誤:無法找到名稱ionViewDidLeave

這裏是我的

ts.file

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

import { NavController, ModalController } from 'ionic-angular'; 
import { EditPost } from '../edit-post/edit-post'; 

import { LoadingController } from 'ionic-angular'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class Home { 

    buttonColor: string = '#787083'; 


    constructor (public navCtrl: NavController, public modalCtrl: ModalController, public loading: LoadingController) { 


//OTHER FUNCTIONS 

    /*Navigate to edit page */ 

    editPost(){ 
     this.buttonColor = '#553481'; //change button background color on click 
      this.navCtrl.push(EditPost, {}) 
      .catch(() => { 
       // Page requires authentication, re-direct to Login page 
       this.navCtrl.setRoot(Login, {routeToPage: 'EditPost'}); 

      }); 

      ionViewDidLeave(){ 

     this.buttonColor = '#787083'; 

      }; 

    }// end of editPost() 

}//close class 

HTML

<ion-footer class="footer"> 
    <ion-segment small class="footer"> 
        <ion-segment-button id="post" value="post" (click)="postEvent()" [ngStyle]="{'background-color': buttonColor}" small> <span class="footer">NEW POST</span></ion-segment-button> 
        <ion-segment-button id="edit" value="edit" (click)="editPost()" [ngStyle]="{'background-color': buttonColor}" small> <span class="footer">Edit Post</span></ion-segment-button > 
    </ion-segment> 
</ion-footer> 

回答

1

當你寫一個方法內

ionViewDidLeave() 

您正在從當前範圍(editPost)函數調用函數。從對象調用正確的方法是:

this.ionViewDidLeave() 

,但我想這不是正確的稱呼它(ionViewDidLeave是離子的頁面生命週期的一部分),我想也想要做的是定義該方法,並且在代碼中有一個類型。正確的代碼應該是:

export class Home { 

    buttonColor: string = '#787083'; 

    constructor (public navCtrl: NavController, public modalCtrl: ModalController, public loading: LoadingController) { 

    editPost(){ 
     this.buttonColor = '#553481'; //change button background color on click 
      this.navCtrl.push(EditPost, {}) 
      .catch(() => { 
       // Page requires authentication, re-direct to Login page 
       this.navCtrl.setRoot(Login, {routeToPage: 'EditPost'}); 
      }); 

    }// end of editPost() 


    ionViewDidLeave(){ 

     this.buttonColor = '#787083'; 

    }; 

}//close class 
相關問題