Skip to content

Commit 0c703e3

Browse files
misoguyyyx990803
authored andcommitted
fix(v-model): v-if / v-else not working with :type + v-model (#6955)
fix #6918
1 parent bdcc05a commit 0c703e3

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-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 hasElse = getAndRemoveAttr(el, 'v-else', true) != null
33+
const elseIfCondition = 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+
if (hasElse) {
66+
branch0.else = true
67+
} else if (elseIfCondition) {
68+
branch0.elseif = elseIfCondition
69+
}
70+
6271
return branch0
6372
}
6473
}

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

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

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

0 commit comments

Comments
 (0)