angular - 如何使用 Ionic 4 检测平台

标签 angular typescript ionic-framework ionic4 platform

如何使用 Ionic 4 检测浏览器和移动网络平台,因为当我在桌面浏览器中尝试使用以下代码时,它没有落入 ‘核心’ 健康)状况。

if (this.platform.is('core')) {
    alert('core platform');
  } else {
    alert('something else');
  }

当我在 chrome 开发人员工具中进行调试时,它显示 '机器人' 平台如下图所示。

enter image description here

谁能帮助我如何检测 Ionic 4 中的平台,或者有什么替代方法?

最佳答案

对于我的用例,我想要区分 nativebrowser平台。也就是说,我的应用程序是在浏览器中运行还是在 native 移动设备中运行。这是我想出的服务:

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';
    }
  }
}

关于angular - 如何使用 Ionic 4 检测平台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51816498/

相关文章:

typescript - 通过Play商店实现付款集成的Ionic应用

javascript - 如果幻灯片包含在自定义组件中,则幻灯片不起作用

node.js - Express 服务器添加 ssl

javascript - 使用 Angular 形式更改表单元素

javascript - 无法在短时间内加载属性 - 导致错误

angular - 等待 firebase 加载快照

angular - 如何重启ngOnInit来更新Interpolation

android - 在 Ionic 中获取连接的 WiFi 网络详细信息

javascript - Angular2 - 组件变量/组件类属性的双向数据绑定(bind)?

Angular:如何将指令微语法与 cdkConnectedOverly 一起使用?