2017-05-30 186 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 
相关问题