首页 > 解决方案 > Ionic - JavaScript:访问函数内的属性

问题描述

这可能是一个愚蠢的问题,但我是初学者,所以请帮助我

我想访问一个公共属性并在setTimeout函数中更改它的值,下面是我的代码。

export class AppComponent implements OnInit {
  public selectedIndex = 0;
  public appPages = [
    
    {
      title: 'Trash',
      url: '/folder/Trash',
      icon: 'trash'
    },
    {
      title: 'Spam',
      url: '/folder/Spam',
      icon: 'warning'
    }
  ];
  public showSplash = true;
  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar,

) {
    this.initializeApp();

  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      setTimeout(function() {
        //showSplash = false ;
        console.log('set timeout worked');
       }, 300);


    });
  }

console.log 在setTimeout内工作我需要更改该块中的showSplash值,请帮助我该怎么做

标签: javascriptionic-framework

解决方案


我认为是角度和打字稿代码。您可以使用箭头功能。

https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Functions/Arrow_functions

initializeApp() {
  this.platform.ready().then(() => {
    this.statusBar.styleDefault();
    this.splashScreen.hide();
    setTimeout(() => {
      this.showSplash = false;
      console.log('set timeout worked');
    }, 300);
  });
}

推荐阅读