首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ion-刷新后切换它必须从本地存储中取回数据

Ion-刷新后切换它必须从本地存储中取回数据
EN

Stack Overflow用户
提问于 2018-09-27 18:42:01
回答 1查看 346关注 0票数 1

所以我有个问题。我的目标是:我在我的应用程序中创建了一个黑色/浅色主题。因此,只要我点击切换,应用程序的主题就会变成黑色,如果我刷新它,它就会保持黑色,如果我把它重新打开,它就会重新变亮,它会一直亮着。但我的努力是,每当我刷新页面时,切换不会停留在当前状态,如在->黑色主题和关闭->轻主题

因此,在->黑色主题关闭->轻主题

Settings.HTML

代码语言:javascript
复制
<ion-item>>
    <ion-label>Light/Dark</ion-label>
    <ion-toggle [(ngModel)]="lightDark" [checked]="" (ionChange)="toggleAppTheme()"></ion-toggle>
</ion-item>

Settings.TS

代码语言:javascript
复制
 constructor(public navCtrl: NavController,
            public navParams: NavParams,
            private settings: SettingsProvider) {
    this.settings.getActiveTheme().subscribe(val => this.selectedTheme = val);

    console.log("Toggled: "+ this.lightDark);
}

toggleAppTheme() {
    if (this.selectedTheme == 'light-theme') {

        this.settings.setActiveTheme('dark-theme');
        localStorage.setItem("themeColor", this.selectedTheme);

    } else if (this.selectedTheme == 'dark-theme') {

        this.settings.setActiveTheme('light-theme');
        localStorage.setItem("themeColor", this.selectedTheme);

    }
}

App.Component.ts

代码语言:javascript
复制
 // Dark/Light Mode
    if(localStorage.getItem("themeColor") == "dark-theme")
    {

        this.settings.setActiveTheme("dark-theme");


    }
    else if(localStorage.getItem("themeColor") == "light-theme")
    {

        this.settings.setActiveTheme("light-theme");

    }
EN

回答 1

Stack Overflow用户

发布于 2018-09-28 16:58:31

ion-toggle组件绑定到lightDark属性,但我没有看到您在初始化页面时设置它的初始值:

代码语言:javascript
复制
constructor(public navCtrl: NavController,
            public navParams: NavParams,
            private settings: SettingsProvider) {

    this.settings.getActiveTheme().subscribe(
        val => {
            this.selectedTheme = val;

            // Initialize the state of the toggle
            // It should be true if the theme is the dark one, right?
            this.lightDark = this.selectedTheme === 'dark-theme';     

            // Show the value in the console to see if it works 
            console.log("Toggled: "+ this.lightDark);       
        },
        error => {
            // Handle the error...
        });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52535151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档