Skip to content

Commit

Permalink
fix(unwrapRef): copy __ob__ and make toRaw work in props (#409)
Browse files Browse the repository at this point in the history
* fix(unwrapRef): copy __ob__ and make toRaw work when passing via props, fix #392

* fix
  • Loading branch information
antfu committed Jun 30, 2020
1 parent 85ffede commit 5f23886
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/reactivity/unwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export function unwrapRefProxy(value: any, map = new WeakMap()) {
(s) => (obj[s] = (value as any)[s])
)

// copy __ob__
if (value.__ob__) {
Object.defineProperty(obj, '__ob__', value.__ob__)
}

for (const k of Object.keys(value)) {
const r = value[k]
// don't process on falsy or raw
Expand Down
27 changes: 27 additions & 0 deletions test/setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,33 @@ describe('setup', () => {
},
})
})

// #392
it('should copy __ob__ and make toRaw work when passing via props', () => {
const Foo = {
template: '<p>{{obj.bar}}</p>',
props: {
obj: {
type: Object,
required: true,
},
},
setup(props) {
expect(toRaw(props.obj)).toEqual({ bar: 1 })
return {}
},
}

const vm = new Vue({
template: '<Foo :obj="obj" />',
components: { Foo },
setup() {
return { obj: { bar: ref(1) } }
},
}).$mount()

expect(vm.$el.textContent).toBe('1')
})
})

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

0 comments on commit 5f23886

Please sign in to comment.