Skip to content

usePrevious ​

类别
导出体积
251 B
上次更改
4 months ago

保存 ref 的先前值。

示例 ​

counter: 1

previous: 0

用法 ​

ts
import { 
usePrevious
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
counter
=
shallowRef
('Hello')
const
previous
=
usePrevious
(
counter
)
console
.
log
(
previous
.
value
) // undefined
counter
.
value
= 'World'
console
.
log
(
previous
.
value
) // Hello

类型声明 ​

ts
/**
 * 保存 ref 的前一个值。
 *
 * @see   {@link https://vueuse.org/usePrevious}
 */
export declare function 
usePrevious
<
T
>(
value
:
MaybeRefOrGetter
<
T
>,
):
Readonly
<
ShallowRef
<
T
| undefined>>
export declare function
usePrevious
<
T
>(
value
:
MaybeRefOrGetter
<
T
>,
initialValue
:
T
,
):
Readonly
<
ShallowRef
<
T
>>

源码 ​

源码 • 演示 • 文档

贡献者 ​

ikxin
Anthony Fu
IlyaL
Anthony Fu
Robin
一纸忘忧
Vida Xie
青椒肉丝

更新日志 ​

7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)