입력하는 동안이 아닌 입력하고 난 뒤
enter
를 누르거나blur
가 해제되었을 때 등 입력을 하고 나서 바뀜
<template>
<h1>{{ msg }}</h1>
<input
type="text"
:value="msg"
@change="msg = $event.target.value" />
<h1> {{ checked }}</h1>
</template>
<script>
export default {
data() {
return {
msg: 'Hello world',
checked: false
}
},
}
</script>
lazy
가 게으르다라는 뜻으로v-model.lazy
를 하면 게으르게 동작한다고 하여 엔터를 누르면 그 때 데이터가 바뀐다.
<template>
<h1>{{ msg }}</h1>
<input
type="text"
v-model.lazy="msg" />
<h1> {{ checked }}</h1>
</template>
<script>
export default {
data() {
return {
msg: 'Hello world',
}
},
}
</script>
v-model
로 바인딩 한 후숫자
를 입력하면type
이string
으로 나온다. 만약 값이 계속 숫자로 입력해야하는 값이면v-model.number
라는수식어
를 붙여type
이number
로 나오게 해준다.
<template>
<h1>{{ msg }}</h1>
<input
type="text"
v-model.number="msg" />
</template>
<script>
export default {
data() {
return {
msg: 123,
}
},
watch: {
msg() {
console.log(typeof this.msg)
}
}
}
</script>
<style>
</style>
trim
은 공백을 지워주는 수식어로 띄어쓰기를 해도 값이 밀리지 않고 그대로 있다. 아래 예시의 경우 띄어쓰기를 주르륵 하고 다시 input 밖에 클릭을 하면 다시 돌아오는데, console에는 찍히지 않는다. 이 이유는 msg에 적힌 'hello world'와 입력된 값이 같기 때문에 아무런 변화가 없다고 생각하여 console 창에는 따로 찍히지 않는다. msg 자체에 변화를 주면 console에 찍힌다.
<template>
<h1>{{ msg }}</h1>
<input
type="text"
v-model.trim="msg" />
</template>
<script>
export default {
data() {
return {
msg: 'hello world',
}
},
watch: {
msg() {
console.log(this.msg)
}
}
}
</script>
<style>
</style>