检查是否作为“离子服务"运行在程序中使用条件 [英] Check if running as "ionic serve" to use a conditional in the program
问题描述
有没有办法在程序中检测 ionic serve
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屋!