跳至主要內容
版本:v8

平台

isPlatform

isPlatform 方法可用於測試您的應用程式是否在特定平台上執行

import { isPlatform } from '@ionic/vue';

isPlatform('ios'); // returns true when running on a iOS device

根據使用者所在的平台,isPlatform(platformName) 將會傳回 true 或 false。請注意,同一個應用程式對於多個平台名稱可能會傳回 true。例如,從 iPad 執行的應用程式對於以下平台名稱會傳回 true:mobile、ios、ipad 和 tablet。此外,如果應用程式是從 Cordova 執行的,則 cordova 將為 true。

getPlatforms

getPlatforms 方法可用於判斷您的應用程式目前在哪些平台上執行。

import { getPlatforms } from '@ionic/vue';

getPlatforms(); // returns ["iphone", "ios", "mobile", "mobileweb"] from an iPhone

根據您所在的裝置,getPlatforms 可以傳回多個值。每個可能的值都是平台的階層結構。例如,在 iPhone 上,它會傳回 mobile、ios 和 iphone。

平台

以下表格列出所有可能的平台值及其對應的說明。

平台名稱說明
android執行 Android 的裝置
capacitor執行 Capacitor 的裝置
cordova執行 Cordova 的裝置
desktop桌上型裝置
electron執行 Electron 的桌上型裝置
hybrid執行 Capacitor 或 Cordova 的裝置
ios執行 iOS 的裝置
ipadiPad 裝置
iphoneiPhone 裝置
mobile行動裝置
mobileweb在行動裝置中執行的網頁瀏覽器
phablet平板手機裝置
pwaPWA 應用程式
tablet平板電腦裝置

自訂平台偵測函數

用於偵測特定平台的函數可以透過在全域 Ionic 設定 中提供替代函數來覆寫。每個函數都以 window 作為參數,並傳回布林值。

createApp(App).use(IonicVue, {
platform: {
/** The default `desktop` function returns false for devices with a touchscreen.
* This is not always wanted, so this function tests the User Agent instead.
**/
desktop: (win) => {
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(win.navigator.userAgent);
return !isMobile;
},
},
});
type PlatformConfig = {
android?: ((win: Window) => boolean) | undefined;
capacitor?: ((win: Window) => boolean) | undefined;
cordova?: ((win: Window) => boolean) | undefined;
desktop?: ((win: Window) => boolean) | undefined;
electron?: ((win: Window) => boolean) | undefined;
hybrid?: ((win: Window) => boolean) | undefined;
ios?: ((win: Window) => boolean) | undefined;
ipad?: ((win: Window) => boolean) | undefined;
iphone?: ((win: Window) => boolean) | undefined;
mobile?: ((win: Window) => boolean) | undefined;
mobileweb?: ((win: Window) => boolean) | undefined;
phablet?: ((win: Window) => boolean) | undefined;
pwa?: ((win: Window) => boolean) | undefined;
tablet?: ((win: Window) => boolean) | undefined;
};