首页 > 解决方案 > 如何从代码中获取当前的离子模式

问题描述

我将 Angular 9 与 ionic5 一起使用,我需要从我的打字稿代码中获取当前使用的模式(字符串mdios可能与平台不同)。所以我的问题是,如果这可以直接从打字稿代码中实现,而无需从标签 htmlmode属性中读取它。

我试图查阅有关Platform styles的官方文档,但这仅考虑 CSS 内部的用法。

标签: angularionic-frameworkionic5

解决方案


找到这些东西的最简单方法是查看 Ionic 的源代码,特别是providers文件夹。在那里,我找到了以下文件:https ://github.com/ionic-team/ionic-framework/blob/master/angular/src/providers/config.ts

该文件公开了Config提供程序,它将公开应用程序的整个配置,包括mode.

请看一下这个有效的 Stackblitz 演示

import { Component, OnInit } from "@angular/core";
import { Config } from "@ionic/angular";

@Component({
  selector: "app-home",
  templateUrl: "./home.page.html",
  styleUrls: ["./home.page.scss"]
})
export class HomePage implements OnInit {
  public mode: string;

  constructor(private config: Config) {}

  ngOnInit() {
    // Replace the mode in the app.module.ts
    // file to test this
    this.mode = this.config.get("mode");
  }
}

推荐阅读