ionic 4错误TypeError:无法读取未定义的属性“就绪”

时间:2019-06-06 10:14:24

标签: angular oop ionic-framework

作为我尝试的好程序员,我安装了短毛绒和哈士奇犬。当我检查短毛绒时,我到达了app.component.ts文件,并且一切都变红了。它不喜欢构造函数中的声明。好吧,没问题,我更改了:

  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,
  ) {
    this.initializeApp();
    }

  private platform: Platform;
  private splashScreen: SplashScreen;
  private statusBar: StatusBar;

  public constructor(
    platform: Platform,
    splashScreen: SplashScreen,
    statusBar: StatusBar,
    ){
     this.initializeApp();
     this.platform = platform;
     this.splashScreen = splashScreen;
     this.statusBar = statusBar;
    }

但是,更改之后,我遇到了所有类型的错误:

  

错误TypeError:无法读取未定义的属性“就绪”

这是指:

  public initializeApp(): void {
    this.platform.ready().then( // <----ERROR
      (): void => {
        this.statusBar.styleDefault();
        this.splashScreen.hide();
      },
    );
  }

然后我也会收到有关Type并分配给any[]

的错误

我的主要问题是为什么?为什么私有实例的构造函数声明可以,但是随后在构造函数外部私下声明它,却仍将其传递给构造函数this.platform,为什么找不到。另外,我如何能在没有抱怨的情况下获得相同的结果。

2 个答案:

答案 0 :(得分:1)

您必须在this.platform = platform;之前致电this.initializeApp();,否则您的私有财产将不确定。您的短毛绒应该可以使用以下代码:

public constructor(
    platform: Platform,
    splashScreen: SplashScreen,
    statusBar: StatusBar,
    ){
     this.platform = platform;
     this.splashScreen = splashScreen;
     this.statusBar = statusBar;
     this.initializeApp();
    }

答案 1 :(得分:1)

  private platform: Platform;
  private splashScreen: SplashScreen;
  private statusBar: StatusBar;

  public constructor(
    platform: Platform,
    splashScreen: SplashScreen,
    statusBar: StatusBar,
    ){
     this.initializeApp(); // <-- initialize app called here.
     this.platform = platform; // <-- platform assigned here.
     this.splashScreen = splashScreen;
     this.statusBar = statusBar;
    }

由于在分配initializeApp之前调用了this.platform,因此它的原始值为undefined。 只需在分配后 后移动initializeApp:

     this.platform = platform; // <-- platform assigned here.
     this.splashScreen = splashScreen;
     this.statusBar = statusBar;
     this.initializeApp(); // <-- initialize app called here.