Skip to content

useWakeLock

类别
导出体积
991 B
上次更改
3 days ago

响应式屏幕唤醒锁定 API。提供了一种防止设备在应用程序需要持续运行时变暗或锁定屏幕的方法。

示例

是否支持: false
激活状态: false

用法

js
import { useWakeLock } from '@vueuse/core'

const { isSupported, isActive, forceRequest, request, release } = useWakeLock()

当调用 request 时,如果文档可见,将请求唤醒锁。否则,请求将排队,直到文档变为可见。如果请求成功,isActive 将为 true。每当文档被隐藏时,isActive 将为 false

当调用 release 时,将释放唤醒锁。如果有排队的请求,将被取消。

要立即请求唤醒锁,即使文档被隐藏,请使用 forceRequest。请注意,如果文档被隐藏,可能会引发错误。

类型声明

typescript
type WakeLockType = "screen"
export interface WakeLockSentinel extends EventTarget {
  type: WakeLockType
  released: boolean
  release: () => Promise<void>
}
export type UseWakeLockOptions = ConfigurableNavigator & ConfigurableDocument
/**
 * 响应式屏幕唤醒锁定 API。
 *
 * @see https://vueuse.org/useWakeLock
 * @param options
 */
export declare function useWakeLock(options?: UseWakeLockOptions): {
  sentinel: ShallowRef<WakeLockSentinel | null, WakeLockSentinel | null>
  isSupported: ComputedRef<boolean>
  isActive: ComputedRef<boolean>
  request: (type: WakeLockType) => Promise<void>
  forceRequest: (type: WakeLockType) => Promise<void>
  release: () => Promise<void>
}
export type UseWakeLockReturn = ReturnType<typeof useWakeLock>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Jelf
Fernando Fernández
OrbisK
Anthony Fu
_Kerman
vuff
HannesOberreiter
wheat
koheing

更新日志

v12.4.0 on 1/10/2025
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.0.0-beta.2 on 7/17/2024
5ca57 - fix: should delay wake lock request if document is hidden (#4055)