// 为 reactive() 方法创建的对象进行清空和赋值
// 无返回值
const handleReactive=(obj: any, item?: any)=> {
// 当 item 有传值时进行赋值,反之清空数据
if (item) {
Object.keys(item).forEach((key) => {
obj[key] = item[key]
})
} else {
Object.keys(obj).forEach((key) => {
obj[key] = null
})
}
}
调用方法:
const aa = reactive({
a: ''
})
setTimeout(() => {
handleReactive(aa, { a: 123 })
console.log(aa) // {a: 123}
}, 2000)
setTimeout(() => {
handleReactive(aa)
console.log(aa) // {a:null}
}, 5000)