Skip to content

Commit 5f23886

Browse files
authored
fix(unwrapRef): copy __ob__ and make toRaw work in props (#409)
* fix(unwrapRef): copy __ob__ and make toRaw work when passing via props, fix #392 * fix
1 parent 85ffede commit 5f23886

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/reactivity/unwrap.ts

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export function unwrapRefProxy(value: any, map = new WeakMap()) {
2727
(s) => (obj[s] = (value as any)[s])
2828
)
2929

30+
// copy __ob__
31+
if (value.__ob__) {
32+
Object.defineProperty(obj, '__ob__', value.__ob__)
33+
}
34+
3035
for (const k of Object.keys(value)) {
3136
const r = value[k]
3237
// don't process on falsy or raw

test/setup.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,33 @@ describe('setup', () => {
696696
},
697697
})
698698
})
699+
700+
// #392
701+
it('should copy __ob__ and make toRaw work when passing via props', () => {
702+
const Foo = {
703+
template: '<p>{{obj.bar}}</p>',
704+
props: {
705+
obj: {
706+
type: Object,
707+
required: true,
708+
},
709+
},
710+
setup(props) {
711+
expect(toRaw(props.obj)).toEqual({ bar: 1 })
712+
return {}
713+
},
714+
}
715+
716+
const vm = new Vue({
717+
template: '<Foo :obj="obj" />',
718+
components: { Foo },
719+
setup() {
720+
return { obj: { bar: ref(1) } }
721+
},
722+
}).$mount()
723+
724+
expect(vm.$el.textContent).toBe('1')
725+
})
699726
})
700727

701728
it('should not unwrap built-in objects on the template', () => {

0 commit comments

Comments
 (0)