Skip to content

fix(v-model): add value to $attrs if not defined in props (fix #9330) #9331

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

Merged
merged 2 commits into from
Feb 4, 2019
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
3 changes: 2 additions & 1 deletion src/core/vdom/create-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ function mergeHook (f1: any, f2: any): Function {
function transformModel (options, data: any) {
const prop = (options.model && options.model.prop) || 'value'
const event = (options.model && options.model.event) || 'input'
;(data.props || (data.props = {}))[prop] = data.model.value
const addTo = (options.props && prop in options.props) ? 'props' : 'attrs'
;(data[addTo] || (data[addTo] = {}))[prop] = data.model.value
const on = data.on || (data.on = {})
const existing = on[event]
const callback = data.model.callback
Expand Down
26 changes: 26 additions & 0 deletions test/unit/features/directives/model-component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,30 @@ describe('Directive v-model component', () => {
expect(triggerCount).toBe(1)
document.body.removeChild(vm.$el)
})

// #9330
it('should add value to $attrs if not defined in props', () => {
const TestComponent = {
inheritAttrs: false,
render (h) {
return h('div', this.$attrs.value)
}
}

const vm = new Vue({
components: {
TestComponent
},
template: `
<div>
<test-component v-model="val"/>
</div>
`,
data: {
val: 'foo'
}
}).$mount()

expect(vm.$el.innerHTML).toBe('<div>foo</div>');
})
})