API 浏览器
平台检测

Quasar 内置了辅助方法,用于检测代码运行的平台(及其功能)。

¥Helpers are built into Quasar to detect the Platform (and its capabilities) within the context of which the code is running.

提示

根据你的需求,你可能还需要查看 风格与标识 > 可见性 页面,了解如何仅使用 CSS 实现相同的效果。后一种方法将渲染你的 DOM 元素或组件,无论平台如何,因此请明智地选择如何处理应用的性能。

¥Based on your needs, you might also want to check the Style & Identity > Visibility page to see how you can achieve the same effect using CSS alone. This latter method will render your DOM elements or components regardless of platform though, so choose wisely on how you want to handle the performance of your app.

Loading Platform API...

用法(Usage)

¥Usage

在 Vue 组件 JS 中的用法:

¥Usage inside a Vue component JS:

import { useQuasar } from 'quasar'

setup () {
  const $q = useQuasar()

  $q.platform.is.mobile
}

在 Vue 组件模板中的用法:

¥Usage inside a Vue component template:

$q.platform.is.cordova

在 Vue 组件之外使用它时,必须导入它:

¥You must import it when you use it outside of a Vue component :

import { Platform } from 'quasar'

Platform.is 本身返回一个包含当前平台详细信息的对象。例如,在 MacOS 台式机上运行 Chrome 时,Platform.is 将返回类似以下内容:

¥Platform.is by itself returns an object containing details about the current platform. For example when running Chrome on a MacOS desktop machine, Platform.is would return something similar to:

{
  chrome: true,
  desktop: true,
  mac: true,
  name: "chrome",
  platform: "mac",
  version: "70.0.3538.110",
  versionNumber: 70,
  webkit: true
}

现在,假设我们想要根据代码运行的平台渲染不同的组件或 DOM 元素。我们希望在桌面上显示一些内容,在移动设备上显示其他内容。我们将按如下方式进行:

¥Now, let’s say we want to render different components or DOM elements, based on the platform that the code is running under. We want to show something on desktop and something else on mobile. We would proceed like this:

<div v-if="$q.platform.is.desktop">
  I'm only rendered on desktop!
</div>

<div v-if="$q.platform.is.mobile">
  I'm only rendered on mobile!
</div>

<div v-if="$q.platform.is.electron">
  I'm only rendered on Electron!
</div>
Your device



属性(Properties)

¥Properties

以下属性可用于 Platform 对象。但这并非详尽的列表。有关更多详细信息,请参阅下面的 API 部分。

¥The following properties are available to the Platform object. It’s not an exhaustive list though. See the API section below for more details.

属性类型含义
Platform.is.mobile布尔值代码是在移动设备上运行的吗?
Platform.is.cordova布尔值代码是在 Cordova 中运行的吗?
Platform.is.capacitor布尔值代码是在 Capacitor 中运行的吗?
Platform.is.nativeMobile布尔值代码是否在原生移动端封装器(Cordova/Capacitor)中运行?
Platform.is.nativeMobileWrapper字符串原生移动端封装器的名称('cordova''capacitor'undefined
Platform.is.electron布尔值代码是否在 Electron 中运行?
Platform.is.desktop布尔值代码是在桌面浏览器上运行的吗?
Platform.is.bex布尔值代码是否在浏览器扩展程序中运行?
Platform.is.android布尔值该应用是否在 Android 设备上运行?
Platform.is.blackberry布尔值该应用是否在 Blackberry 设备上运行?
Platform.is.cros布尔值该应用是否在搭载 Chrome OS 操作系统的设备上运行?
Platform.is.ios布尔值该应用是否在 iOS 设备上运行?
Platform.is.ipad布尔值该应用是否在 iPad 上运行?
Platform.is.iphone布尔值该应用是否在 iPhone 上运行?
Platform.is.ipod布尔值该应用是否在 iPod 上运行?
Platform.is.kindle布尔值该应用是否在 Kindle 设备上运行?
Platform.is.linux布尔值代码是在 Linux 操作系统的设备上运行的吗?
Platform.is.mac布尔值代码是在 MacOS 操作系统的设备上运行的吗?
Platform.is.win布尔值代码是在 Windows 操作系统的设备上运行的吗?
Platform.is.winphone布尔值代码是在 Windows Phone 设备上运行的吗?
Platform.is.playbook布尔值代码是在 Blackberry Playbook 设备上运行的吗?
Platform.is.silk布尔值代码是在 Kindle Silk 浏览器上运行的吗?
Platform.is.chrome布尔值代码是否在 Google Chrome 浏览器中运行?
Platform.is.firefox布尔值代码是否在 Firefox 浏览器中运行?
Platform.is.opera布尔值代码是在 Opera 浏览器中运行的吗?
Platform.is.safari布尔值代码是否在 Apple Safari 浏览器中运行?
Platform.is.vivaldi布尔值代码是在 Vivaldi 浏览器中运行的吗?
Platform.is.edge布尔值代码是否在 Microsoft Edge 浏览器中运行?
Platform.is.ie布尔值代码是否在 Microsoft Internet Explorer 浏览器中运行?
Platform.is.webkit布尔值代码是在 Webkit 还是基于 Webkit 的浏览器中运行的?
Platform.has.touch布尔值代码是在触摸屏上运行的吗?
Platform.within.iframe布尔值该应用是否在 IFRAME 中运行?

提示

在移动设备上运行意味着你可以在移动设备(手机或平板电脑)上使用浏览器运行此代码,而不是在 Cordova 封装器中运行。

¥Running on mobile means you can have this code running on a mobile device (phone or tablet) but with a browser, not within a Cordova wrapper.

关于服务器渲染的说明(Note about SSR)

¥Note about SSR

构建 SSR 应用时,请仅使用 $q.platform 表单。或者,在服务器端时,这是另一个如何使用它的示例:

¥When building for SSR, use only the $q.platform form. Alternatively, when on server-side, this is one more example of how you can use it:

import { Platform } from 'quasar'

// you need access to `ssrContext`
function (ssrContext) {
  const platform = process.env.SERVER
    ? Platform.parseSSR(ssrContext)
    : Platform // otherwise we're on client

  // platform is equivalent to the global import as in non-SSR builds
}

ssrContext 可在 @quasar/app-vite Boot File@quasar/app-webpack Boot File 中使用。在 @quasar/app-vite preFetch@quasar/app-webpack preFetch 功能中,它作为参数提供。

¥The ssrContext is available in @quasar/app-vite Boot File or @quasar/app-webpack Boot File. And also in the @quasar/app-vite preFetch or @quasar/app-webpack preFetch feature, where it is supplied as a parameter.

这样做的原因是,在纯客户端应用中,每个用户都会在其浏览器中使用应用的新实例。对于服务器端渲染,我们想要的是相同的:每个请求都应该有一个全新的、独立的应用实例,这样就不会出现跨请求状态污染。因此 Platform 需要单独绑定到每个请求。

¥The reason for all this is that in a client-only app, every user will be using a fresh instance of the app in their browser. For server-side rendering we want the same: each request should have a fresh, isolated app instance so that there is no cross-request state pollution. So Platform needs to be bound to each request separately.