Check if running as “ionic serve” to use a conditional in the program

寵の児 提交于 2019-12-10 03:17:11

问题


Is there a way to detect that ionic serve CLI is running (and then not on a real device) in the program and use it as a conditional.

My issue: I have a Cordova plugin that sends back an answer to Cordova.

When I was using Ripple, it offered to chose the value of the callback to be sent back to JavaScript (to emulate the plugin result).

I notice that Ionic2 does not do that when I run in a browser. So to ease my dev and make it possible to test it on a browser (and not build to the real device constantly), I would like to be able to check in the program if the ionic serve CLI is running. In other words: Check if it is running on a real device or a regular browser.

If it runs on the browser then I'd used a prompt to ask the user to enter a dummy value for the Cordova plugin result instead of the real Cordova plugin.


回答1:


See the Platform doc:

Check if it is running on a real device or a regular browser.

You can use the platform information in order to do so:

Platform Name   Description
android         on a device running Android.
cordova         on a device running Cordova.
core            on a desktop device.
ios             on a device running iOS.
ipad            on an iPad device.
iphone          on an iPhone device.
mobile          on a mobile device.
mobileweb       in a browser on a mobile device.
phablet         on a phablet device.
tablet          on a tablet device.
windows         on a device running Windows.

So you can do something like this:

import { Platform } from 'ionic-angular';

@Component({...})
export MyPage {
  constructor(public platform: Platform) {

    if (this.platform.is('mobileweb') || this.platform.is('core')) {
      // This will only print when running on desktop
      console.log("I'm a regular browser!");
    }
  }
}


来源:https://stackoverflow.com/questions/40301003/check-if-running-as-ionic-serve-to-use-a-conditional-in-the-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!