复制的对象无法在控制台中打印_rawValue


const a = ref(new A()); 
const test = () => {
const b = { ...a };
console.log(a); //  RefImpl{_v_isShallow: false, _dep: sth, _v_isRef: true, _rawValue: sth, _value: sth} 
console.log(b); // {_v_isShallow: false, _dep: sth, _v_isRef: true, _rawValue: sth, _value: sth} 
console.log(a.value); prints value 
console.log(b.value); undefined
}

除了a被包裹在RefImpl中之外,来自两个console.log(a), console.log(b){}中的所有内容都是相同的

我不知道为什么b.value是未定义的。

有什么建议吗?

提前感谢您的帮助!

通常,最好避免像这样直接将ref对象相互复制,因为预期用途是引用可以直接分配或操作的单个元素/值。

如果你想复制一个副本,这样你就有两个不同的引用,它们共享相同的值,但可以单独修改而不会相互影响,你可以这样实例化-

const a = ref(new A());
const b = ref(a.value);

最新更新