angular - 如何使用 Ionic 4 检测平台
问题描述
解决方案
对于我的用例,我想要一些东西来区分native
和browser
平台。也就是说,我的应用程序是在浏览器中运行还是在本机移动设备中运行。这是我想出的服务:
import { Injectable } from '@angular/core';
import {Platform} from '@ionic/angular';
type CurrentPlatform = 'browser' | 'native';
@Injectable({
providedIn: 'root'
})
export class CurrentPlatformService {
private _currentPlatform: CurrentPlatform;
constructor(private platform: Platform) {
this.setCurrentPlatform();
}
get currentPlatform() {
return this._currentPlatform;
}
isNative() {
return this._currentPlatform === 'native';
}
isBrowser() {
return this._currentPlatform === 'browser';
}
private setCurrentPlatform() {
// Are we on mobile platform? Yes if platform is ios or android, but not desktop or mobileweb, no otherwise
if (
this.platform.is('ios')
|| this.platform.is('android')
&& !( this.platform.is('desktop') || this.platform.is('mobileweb') ) ) {
this._currentPlatform = 'mobile';
} else {
this._currentPlatform = 'browser';
}
}
}
推荐阅读
- java - 如何更改“按ESC退出全屏”
- intellij-idea - 如何在 IntelliJ 中将 catch 块添加到现有的 try catch 块
- css - Angular 没有完全加载样式
- c# - 超链接控件在一个目录中打开一个文件,而不是在另一个目录中打开同一个文件
- mysql - Magento cron 或 mysql 事件调度器哪一个基于性能最好?
- php - 如何检查事件是否已执行?
- android - Flutter 如何使用 AssetBundle 存储和访问文件
- ruby-on-rails - Draft.js 在 Rails 应用程序视图中未显示任何内容
- javascript - Ajax Uncaught TypeError:非法调用
- javascript - 如何在 reactjs 中访问自定义标头