Skip to content

Commit c166df3

Browse files
committed
fix(model): fix dynamic v-model with v-else statement
fix vuejs#6918
1 parent d1d8b58 commit c166df3

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/platforms/web/compiler/modules/model.js

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ function preTransformNode (el: ASTElement, options: CompilerOptions) {
2929
const typeBinding: any = getBindingAttr(el, 'type')
3030
const ifCondition = getAndRemoveAttr(el, 'v-if', true)
3131
const ifConditionExtra = ifCondition ? `&&(${ifCondition})` : ``
32+
const elseStmt = getAndRemoveAttr(el, 'v-else', true)
33+
const elseIfStmt = getAndRemoveAttr(el, 'v-else-if', true)
3234
// 1. checkbox
3335
const branch0 = cloneASTElement(el)
3436
// process for on the main node
@@ -59,6 +61,13 @@ function preTransformNode (el: ASTElement, options: CompilerOptions) {
5961
exp: ifCondition,
6062
block: branch2
6163
})
64+
65+
// v-else and v-else-if
66+
if (elseStmt != null) {
67+
branch0.else = true
68+
} else if (elseIfStmt) {
69+
branch0.elseif = elseIfStmt
70+
}
6271
return branch0
6372
}
6473
}

test/unit/features/directives/model-dynamic.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,51 @@ describe('Directive v-model dynamic input type', () => {
4040
assertInputWorks(vm, chain).then(done)
4141
})
4242

43+
it('with v-else-if', done => {
44+
const vm = new Vue({
45+
data: {
46+
foo: true,
47+
bar: false,
48+
type: null,
49+
test: 'b'
50+
},
51+
template: `<div v-if="foo">text</div><input v-else-if="bar" :type="type" v-model="test">`
52+
}).$mount()
53+
document.body.appendChild(vm.$el)
54+
55+
const chain = waitForUpdate(() => {
56+
expect(vm.$el.textContent).toBe('text')
57+
}).then(() => {
58+
vm.foo = false
59+
}).then(() => {
60+
expect(vm._vnode.isComment).toBe(true)
61+
}).then(() => {
62+
vm.bar = true
63+
})
64+
65+
assertInputWorks(vm, chain).then(done)
66+
})
67+
68+
it('with v-else', done => {
69+
const vm = new Vue({
70+
data: {
71+
ok: true,
72+
type: null,
73+
test: 'b'
74+
},
75+
template: `<div v-if="ok">text</div><input v-else :type="type" v-model="test">`
76+
}).$mount()
77+
document.body.appendChild(vm.$el)
78+
79+
const chain = waitForUpdate(() => {
80+
expect(vm.$el.textContent).toBe('text')
81+
}).then(() => {
82+
vm.ok = false
83+
})
84+
85+
assertInputWorks(vm, chain).then(done)
86+
})
87+
4388
it('with v-for', done => {
4489
const vm = new Vue({
4590
data: {

0 commit comments

Comments
 (0)