检查是否以“离子发球”的形式运行在程序中使用条件 [英] Check if running as "ionic serve" to use a conditional in the program
问题描述
有没有办法在程序中检测离子服务
CLI正在运行(然后不在真实设备上)并使用它作为条件。
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.
我的问题:我有一个Cordova插件,可以回复Cordova的答案。
My issue: I have a Cordova plugin that sends back an answer to Cordova.
当我使用Ripple时,它提供选择将回调的值发送回JavaScript(以模拟插件结果)。
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).
我注意到Ionic2在浏览器中运行时没有这样做。因此,为了简化我的开发并使其能够在浏览器上进行测试(而不是不断构建到真实设备),我希望能够在程序中检查 ionic serve
CLI正在运行。 换句话说:检查它是在真实设备还是常规浏览器上运行。
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.
如果它在浏览器上运行,那么我就是使用提示要求用户输入Cordova插件结果的虚拟值而不是真正的Cordova插件。
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.
推荐答案
参见平台文档:
检查它是在真实设备还是普通浏览器上运行。
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.
所以你可以这样做:
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!");
}
}
}
这篇关于检查是否以“离子发球”的形式运行在程序中使用条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!