错误:找不到名称ionViewDidLeave

时间:2017-05-30 19:35:51

标签: view navigation ionic2 event-listener

我不知道ionViewDidLeave在哪里出错了。我收到来自终端的错误,上面写着"找不到姓名ionViewDidLeave"。有什么东西我必须导入才能使用它吗?我已经导入了navController。

这是我的

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 个答案:

答案 0 :(得分:1)

在内部编写方法时

ionViewDidLeave()

您正在调用当前作用域(editPost)函数中的函数。从对象调用的正确方法是:

this.ionViewDidLeave()

但是我觉得调用它是不对的(ionViewDidLeave是Ionic页面生命周期的一部分),我想你想要做的是定义这个方法,你的代码中有一个类型。正确的代码应该是:

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