angular - 如何从代码中获取当前的离子模式
问题描述
我将 Angular 9 与 ionic5 一起使用,我需要从我的打字稿代码中获取当前使用的模式(字符串md
或ios
可能与平台不同)。所以我的问题是,如果这可以直接从打字稿代码中实现,而无需从标签 htmlmode
属性中读取它。
我试图查阅有关Platform styles的官方文档,但这仅考虑 CSS 内部的用法。
解决方案
找到这些东西的最简单方法是查看 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");
}
}
推荐阅读
- c# - 使用 Vector3.Distance() 仅计算 2 个不同变换的 x 轴之间的距离
- hibernate - [Ljava.lang.Object; 不能转换为 ''
- reporting-services - SSRS 2017 Web 门户表单身份验证访问问题
- go - 使用 Golang 如何获取包含元数据的数据集表列表
- android - 同步网络与异步网络的优缺点是什么?
- javascript - 节点 JS 表示 UNDEFIED CALL BACK
- python-3.x - Np.Linspace 给了我空数组
- azure - Azure:应用服务:部署虚拟目录
- python - Tkinter GUI 使用带有函数的单独 Python 文件实现
- java - 在 main 中调用方法