Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
组件中的Angular 2和Ionic 3生命周期完成_Angular_Ionic2_Ionic3 - Fatal编程技术网

组件中的Angular 2和Ionic 3生命周期完成

组件中的Angular 2和Ionic 3生命周期完成,angular,ionic2,ionic3,Angular,Ionic2,Ionic3,我对Angular 2和Ionic 3中组件的生命周期非常困惑 我有以下组件 import {Component} from '@angular/core'; import { MemberService } from '../../services/members/member.service'; import { NavController, NavParams } from 'ionic-angular'; import { Storage } from '@ionic/storage';

我对Angular 2和Ionic 3中组件的生命周期非常困惑

我有以下组件

import {Component} from '@angular/core';
import { MemberService } from '../../services/members/member.service';
import { NavController, NavParams } from 'ionic-angular';
import { Storage } from '@ionic/storage';

@Component({
    selector: 'page-profile',
  templateUrl: 'profile.html',
})
export class ProfilePage {
    originPost: string = "mine";
  profile: any;
  passed_profile: any;
  username: any;
  user_profile: any;
  following_text: string;
  storage: Storage = new Storage();
  // We need to inject AuthService so that we can
  // use it in the view



  constructor(public memberAuth: MemberService, public navCtrl: NavController, public params: NavParams) {
    this.passed_profile = params.get('profile');
    this.storage.get('profile').then(profile => {
      this.user_profile = profile;
    });
  }

  ionViewCanEnter() {
    if(this.passed_profile) {
      this.profile = this.passed_profile;

    } else {
        this.getProfileDetails({username: this.user_profile.username});

    }

    if(this.profile.username != this.user_profile.username) {
      this.following_text = this.profile.is_following ? 'Following' : 'Follow';
    }
  }

  ionViewDidLoad() {
    console.log("profile page has loaded")
  }


  getProfileDetails(username) {
    this.memberAuth.get_profile(username).subscribe((profile) => {
      this.profile = profile;
    })
  }
}
这段代码在运行之前看起来很棒,但它并没有按预期运行

根据文档,组件中的生命周期如下所示:构造函数->IonViewCanEnter,但我的代码中发生的是,在进入概要文件页面时,构造函数在完成之前运行,它开始运行ionViewCanEnter方法,因此我的页面上出现错误,因为ionViewCanEnter方法中需要一些数据this.user\u配置文件,但由于构造函数方法尚未完成其代码的运行,因此ionViewCanEnter中this.user\u配置文件为空或为零

在进入ionViewCanEnter方法之前,有点困惑如何确保构造函数方法已完成


谢谢你说得对。生命周期是构造函数->IonViewCanCenter。但在您的构造函数中有一个异步函数。可能在运行IonViewCanCenter之前尚未完成。 尝试将IonViewCanEnter中的所有代码移动到IonViewDiCenter。如果仍然不起作用,则应将代码放入其中。然后按如下方式运行:

ionViewDidEnter(){
  this.storage.get('profile').then(profile => {
      this.user_profile = profile;
      //Now you can use this.user_profile
      this.profile = this.passed_profile;
      ...
  });
}