我正在浏览器上运行一个离子项目
脚本
"browser": "ionic-app-scripts serve --sourceMap source-map --iscordovaserve --wwwDir platforms/browser/www/ --buildDir platforms/browser/www/build"
并跑步
npm run browser
我正在尝试这段代码
app.component.ts
if (this.platform.is('ios') || this.platform.is('android') || this.platform.is('mobile'))
//execute certain function only on mobile
问题
this.platform.is('mobile')
和 this.platform.is('android')
正在向我返回 true
,以防浏览器出现奇怪的行为。
那么,如何获取条件以便在浏览器上我想禁用某些功能以及为什么上面的代码不能按预期工作。 谢谢。
我有两种方法。
按照谢尔盖的建议
let isWebApp = this.platform.url().startsWith('http');
在 Ionic 论坛上找到
!document.URL.startsWith('http');
这些主要用于开发,因为开发过程在浏览器中更快,因为更改会立即反映出来。
我真的不认为你需要这个,但你可以使用:
if(this.platform.is('core') || this.platform.is('mobileweb')) {
// In Browser
} else {
// In Mobile
}
您可以使用其他方式来检测您的应用程序是网络应用程序还是cordova混合应用程序:
let isWebApp = this.platform.url().startsWith('http');
在cordova中(基本上在设备上),根据平台,应用程序的“url”将从“file://”或其他但不是http开始。
您只能在移动应用程序上尝试此操作
!isPlatform('mobileweb') && isPlatform('mobile')