Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(unwrapRef): copy __ob__ and make toRaw work in props #409

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__) {
pikax marked this conversation as resolved.
Show resolved Hide resolved
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