主题
useWakeLock
反应式 屏幕唤醒锁定 API。提供一种方法来防止设备在应用需要继续运行时变暗或锁定屏幕。
¥Reactive Screen Wake Lock API. Provides a way to prevent devices from dimming or locking the screen when an application needs to keep running.
示例
用法
¥Usage
js
import { useWakeLock } from '@vueuse/core'
const { isSupported, isActive, forceRequest, request, release } = useWakeLock()
当调用 request
时,如果文档可见,将请求唤醒锁。否则,请求将排队,直到文档变得可见。如果请求成功,isActive
将为真。每当文档被隐藏时,isActive
将为假。
¥When request
is called, the wake lock will be requested if the document is visible. Otherwise, the request will be queued until the document becomes visible. If the request is successful, isActive
will be true. Whenever the document is hidden, the isActive
will be false.
当调用 release
时,唤醒锁将被释放。如果有排队请求,它将被取消。
¥When release
is called, the wake lock will be released. If there is a queued request, it will be canceled.
要立即请求唤醒锁,即使文档被隐藏,也使用 forceRequest
。请注意,如果文档被隐藏,这可能会引发错误。
¥To request a wake lock immediately, even if the document is hidden, use forceRequest
. Note that this may throw an error if the document is hidden.
类型声明
typescript
type WakeLockType = "screen"
export interface WakeLockSentinel extends EventTarget {
type: WakeLockType
released: boolean
release: () => Promise<void>
}
export type UseWakeLockOptions = ConfigurableNavigator & ConfigurableDocument
/**
* Reactive Screen Wake Lock 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>