Skip to content

useCloned ​

类别
导出大小
289 B
最近修改
7 months ago

引用的响应式克隆。默认情况下,它使用 JSON.parse(JSON.stringify()) 进行克隆。

🌐 Reactive clone of a ref. By default, it use JSON.parse(JSON.stringify()) to do the clone.

示例 ​

用法 ​

🌐 Usage

ts
import { 
useCloned
} from '@vueuse/core'
const
original
=
ref
({
key
: 'value' })
const {
cloned
} =
useCloned
(
original
)
original
.
value
.
key
= 'some new value'
console
.
log
(
cloned
.
value
.
key
) // 'value'

对源的更改不会立即反映在克隆引用中。使用 { flush: 'sync' } 可以立即获取更新的值。

🌐 Changes to the source are not reflected in the cloned ref immediately. Use { flush: 'sync' } to obtain the updated value without delay.

ts
const { 
cloned
} = useCloned(original, {
flush
: 'sync' })
original.value.key = 'some new value'
console
.
log
(
cloned
.value.key) // 'some new value'
js
'use strict'
const { cloned } = useCloned(original, { flush: 'sync' })
original.value.key = 'some new value'
console.log(cloned.value.key) // 'some new value'

手动克隆 ​

🌐 Manual cloning

ts
import { 
useCloned
} from '@vueuse/core'
const
original
=
ref
({
key
: 'value' })
const {
cloned
,
sync
} =
useCloned
(
original
, {
manual
: true })
original
.
value
.
key
= 'manual'
console
.
log
(
cloned
.
value
.
key
) // 'value'
sync
()
console
.
log
(
cloned
.
value
.
key
)// 'manual'

自定义克隆函数 ​

🌐 Custom Clone Function

以 klona 为例:

🌐 Using klona for example:

ts
import { 
useCloned
} from '@vueuse/core'
import {
klona
} from 'klona'
const
original
=
ref
({
key
: 'value' })
const {
cloned
,
isModified
,
sync
} =
useCloned
(
original
, {
clone
:
klona
})