javascript 检查离子应用程序是否处于开发服务模式(浏览器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30109230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:42:30  来源:igfitidea点击:

Check if ionic app is in dev serve mode(browser)

javascriptbrowserionic-frameworkionic

提问by Shalev Shalit

I use ionic serveto run my app on localhost.

我用来ionic serve在本地主机上运行我的应用程序。

how can I know when I'm in browserand not in android?

我怎么知道我什么时候在浏览器中而不是在android 中

I tried:

我试过:

navigator.platform // MacIntel
navigator.platforms // undefined
ionic.Platform.is('BROWSER') // false
navigator.userAgent // ...iPhone... => i'm in chrome device mode

Thank you!

谢谢!

回答by Mirko N.

There's probably more than one way to do it, but a simple one is that cordovawill only be defined on Android/iOS so you could do

可能有不止一种方法可以做到,但一个简单的方法是cordova仅在 Android/iOS 上定义,因此您可以这样做

if (window.cordova) {
  // running on device/emulator
} else {
  // running in dev mode
}

Edit

编辑

Some text editors and TypeScript parsers may complain that Property 'cordova' does not exist on type 'Window'.In order to work around that, you can use the following:

一些文本编辑器和 TypeScript 解析器可能会抱怨Property 'cordova' does not exist on type 'Window'.为了解决这个问题,您可以使用以下方法:

if ((<any>window).cordova) {
  // running on device/emulator
} else {
  // running in dev mode
}

By explicitly casting to type anyyou can avoid transpiler errors, and still accomplish what you're trying to do.

通过显式转换为类型,any您可以避免转译器错误,并且仍然可以完成您想要做的事情。

回答by Ygalbel

I found I can use

我发现我可以使用

ionic.Platform.platforms[0] == "browser"

to check if the application is running in a browser or not.

检查应用程序是否在浏览器中运行。

Important thing, ionic.Platform.platformsis set only after $ionicPlatform.readyevent is fired.

重要的是,ionic.Platform.platforms只有在$ionicPlatform.ready事件被触发后才设置。

回答by Hyman Vial

Checking if window.location.hostname is equal to localhost will work too.

检查 window.location.hostname 是否等于 localhost 也可以。

if(window.location.hostname === "localhost"){
    return 'http://localhost:8100/api/';
} else {
    return 'https://some-api.com/';
}

回答by Jess Patton

use

利用

if(ionic.Platform.isWebView()){
  console.log('i am in a browser webview!');
}

or

或者

console.log(ionic.Platform.platform());

That will tell you what platform you are on. Webview or android or ios or whatever.

这会告诉你你在哪个平台上。Webview 或 android 或 ios 或其他。

回答by felipeaf

For now, in my Ionic 1 app (using last version 3.9.x, but with --type ionic1), ionic.Platform.platform() when i am in desktop is returning "linux", not "browser". And window.cordova now exists in browser, but you can check if window.cordova.platformId == 'browser'. It seems work here.

现在,在我的 Ionic 1 应用程序(使用最新版本 3.9.x,但使用 --type ionic1)中,ionic.Platform.platform() 当我在桌面时返回“linux”,而不是“浏览器”。window.cordova 现在存在于浏览器中,但您可以检查 window.cordova.platformId == 'browser'。似乎在这里工作。

回答by ProllyGeek

Just a little correction to Mirko N.answer.

Mirko N. 的回答稍作修正。

Typescript will actually return error if you use cordova directly or as a child of window object.

如果您直接使用cordova 或作为window 对象的子对象,Typescript 实际上会返回错误。

The proper answer is to check if window has cordova as own property.

正确的答案是检查 window 是否将cordova作为自己的财产。

if(window.cordova) //returns error "Property 'cordova' does not exist on type 'Window'."


if(window.hasOwnProperty('cordova')) //Proper Check

enter image description here

在此处输入图片说明