javascript - Ionic 2 - 动态改变状态栏的颜色

标签 javascript cordova ionic-framework ionic2

我正在尝试根据获取的参数在 ionic 页面中更改状态栏颜色。

只有 onDeviceReady() 函数中的 colorStatusBar 变量显示为 undefined

有人可以帮我解决这个问题吗?

typeColor: string;
colorStatusBar: string;

constructor(public navCtrl: NavController, public navParams: NavParams, statusBar: StatusBar){
    this.typeColor = this.navParams.get('type');
    if(this.typeColor == "value1"){
      this.colorStatusBar = "#F44336";
    }
    if(this.typeColor == "value2"){
      this.colorStatusBar = "#66BB6A";
    }
    if(this.typeColor == "value3"){
      this.colorStatusBar = "#9E9E9E";
    }

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
          console.log(this.colorStatusBar) // undefined
          statusBar.backgroundColorByHexString(this.colorStatusBar);
    }
}

最佳答案

我只能让它按如下方式工作:

import { Component } from '@angular/core';
import { NavController, NavParams, Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';

...

typeColor: string;
constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public statusBar: StatusBar,
    public platform: Platform){

        this.typeColor = this.navParams.get('type');
        this.platform.ready().then(() => {
            if(this.typeColor == "value1"){
                statusBar.backgroundColorByHexString("#F44336");
            }
            if(this.typeColor == "value2"){
                statusBar.backgroundColorByHexString("#66BB6A");
            }
            if(this.typeColor == "value3"){
                statusBar.backgroundColorByHexString("#9E9E9E");
            }
        })
}

...

关于javascript - Ionic 2 - 动态改变状态栏的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44446369/

相关文章:

android - 无法接受许可协议(protocol) Android SDK Platform 24

javascript - firebase.database() 不是函数 - 新的 Firebase 项目

javascript - 为什么 Javascript 将 5 == 8 等同于 true?

html - 在 android 浏览器/cordova 上滚动太远时摆脱阴影

javascript - ionic cli 在 grunt 服务后退出

css - <ion-card-title> 中的文本溢出省略号不起作用 --> Shadow-Dom-Element 不占用父级宽度

angularjs - 与 Ionic Apps 一起使用的最佳富文本编辑器是什么?

javascript - Discord 机器人对话

javascript - 通过正则表达式提取子字符串

ios - 关闭 Cordova iOS 应用程序时,安全 cookie 会丢失