v-if
<body>
<style>
.box {
width: 100px;
height: 100px;
}
.box--red {
background-color: red;
}
.box--blue {
background-color: blue;
}
.box--gray {
background-color: gray;
}
</style>
<div id="app">
<div v-if="colorState === 'red'" class="box box--red"></div>
<div v-if="colorState === 'blue'" class="box box--blue"></div>
<div v-if="colorState !== 'red' && colorState != 'blue'" class="box box--gray"></div>
</div>
<script>
const vm = new Vue({
el: '#app',
data: {
colorState: 'grdfday'
},
})
</script>
</body>
v-if
, v-else
, v-if-else
v-if
는 토글비용이 높다
- 실질적으로 요소를 없애버린다
v-show
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<style>
.box {
width: 100px;
height: 100px;
background-color: orange;
}
</style>
<div id="app">
<button @click="toggle">Toggle</button>
<div v-show="show" class="box"></div>
</div>
<script>
const vm = new Vue({
el: '#app',
data: {
show: true
},
methods: {
toggle() {
this.show = !this.show
}
}
})
</script>
</body>
</html>
- 초기 렌더링 비용이 높다
- CSS로
display: none;
을 적용시켜 요소를 없애지 않고 요소가 보이지 않게 한다