Skip to content

usePrevious

保存 ref 的前一个值。

¥Holds the previous value of a ref.

用法

¥Usage

ts
import { ref } from 'vue'
import { usePrevious } from '@vueuse/core'

const counter = ref('Hello')
const previous = usePrevious(counter)

console.log(previous.value) // undefined

counter.value = 'World'

console.log(previous.value) // Hello

类型声明

typescript
/**
 * Holds the previous value of a ref.
 *
 * @see   {@link https://vueuse.org/usePrevious}
 */
export declare function usePrevious<T>(
  value: MaybeRefOrGetter<T>,
): Readonly<Ref<T | undefined>>
export declare function usePrevious<T>(
  value: MaybeRefOrGetter<T>,
  initialValue: T,
): Readonly<Ref<T>>

源代码

源代码文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号