主题
usePrevious
保存 ref 的前一个值。
¥Holds the previous value of a ref.
用法
¥Usage
ts
import { usePrevious } from '@vueuse/core'
import { ref } from 'vue'
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>>