Check if running as “ionic serve” to use a conditi

2019-04-24 04:06发布

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条回答
来,给爷笑一个
2楼-- · 2019-04-24 04:50

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!");
    }
  }
}
查看更多
登录 后发表回答