const todos = ref([
{ text : 'Vue 실습'},
{ text : '자격증 공부'},
{ text : 'TIL 작성'}
])
<h2> 남은 할 일 </h2>
<p> {{todos.length > 0 ? '아직남았다':'퇴근!'}}</p>
const { createApp, ref, computed } = Vue
const restOfTodos = computed(() => {
return todos.value.length >0 ? '아직 남았다':'퇴근!'
})
const isSeen = ref(true)
<p v-if='isSeen'>true일 때 보여요</p>
<p v-else>flase일 때 보여요</p>
<button @click='isSeen =!isSeen'>토글</button>
Vue.js에서 @click는 이벤트 처리기를 정의하는 지시자이다.
이 구문은 사용자가 해당 버튼을 클릭할 때 어떤 동작을 수행해야 하는지를 정의한다.
@click 지시자 뒤에 오는 표현식은 클릭 이벤트가 발생했을 때 실행될 JavaScript 코드를 지정함.
@click='isSeen = !isSeen'는 버튼이 클릭됐을 때 isSeen 변수의 값을 토글하는 코드를 실행한다.
isSeen 변수의 초기값은 true이며, 클릭 이벤트가 발생하면 !isSeen 표현식을 사용하여 isSeen의 현재 값의 반대 값을 할당한다.
즉, isSeen이 true이면 클릭 후에는 false가 되고, false이면 클릭 후에는 true가 된다.
이것은 "토글" 버튼을 클릭할 때 상태를 전환하는 동작을 수행한다.
그러므로 "토글" 버튼을 클릭할 때, 두 개의 <p> 요소 중 하나가 나타나고 다른 하나가 숨겨진다.
const name = ref('Cathy')
<div v-if="name === 'Alice'">Alice입니다</div>
<div v-else-if="name === 'Bella'">Bella입니다.</div>
<div v-else-if="name === 'Cathy'">Cathy입니다.</div>
<div v-else>아무도 아닙니다.</div>
<template v-if="name === 'Cathy'">
<div>Cathy입니다</div>
<div>나이는 30살입니다.</div>
</template>
const isShow = ref(false)
<div v-show='isShow'>v-show</div>
소스 데이터를 기반으로 요소 또는 템플릿 블록을 여러번 렌더링
v-for은 alias in expression 형식의 특수 구문을 사용하여 반복되는 현재 요소에 대한 별칭을 제공한다
<div v-for='item in items'>
{{item.text}}
</div>
<div v-for="(item, index) in items"></div>
<div v-for='value in object></div>
<div v-for='(value, key) in object></div>
<div v-for='(value,key, index) in object></div>
const myArr = ref([
{name: 'Alice', age:20},
{name: 'Bella', age:21}
])
<div v-for="(item, index) in myArr">
{{ index }} / {{ item }}
</div>
const myObj = ref({
name: 'Cathy',
age: 30
})
<div v-for="(value, key, index) in myObj">
{{ index }} / {{ key }} / {{ value }}
</div>
<ul>
<template v-for="item in myArr">
<li>{{item.name}}</li>
<li>{{item.age}}</li>
<hr>
</template>
</ul>
const myInfo = ref([
{name: 'Alice', age:20, friends: ['Bella, 'Cathy', 'Dan']},
{name: 'Bella', age:21, friends: ['Alice', 'Cathy']}
])
<ul v-for='item in myInfo'>
<li v-for='friend in item.friends'>
{{item.name}} - {{friend}}
</li>
</ul>
let id = 0
const items = ref([
{id: id++, name:'Alice'},
{id: id++, name:'Bella'},
])
<div v-for='item in items' :key='item.id'>
<!--content-->
</div>
const completeTodos = computed(() => {
return todos.value.filter((todo) => !todo.isComplete)
})
<ul>
<li v-for="todo in completTodos" :key="todo.id">
{{ todo.name }}
</li>
</ul>
<ul>
<template v-for="todo in todos" :key="todo.id">
<li v-if="!todo.isComplete">
{{todo.name}}
</li>
</template>
</ul>
watch(variable,(newValue, oldValue) => {
//do something
}
variable
newValue
oldValue
예시1
<button @click='count++'>Add 1</button>
<p>Count: {{count}}</p>
const count = ref(0)
const countWatch = watch(count, (newValue, oldValue) => {
console.log(`newValue: ${newValue}, oldValue: ${oldValue}`)
})
<input v-model='message'>
<p>Message length : {{messageLength}}</p>
const message = ref('')
const messageLength = ref(0)
const messageWatch = watch(message, (newValue,oldValue) => {
messageLength.value = newValue.length
})
Vue 인스턴스의 생애주기 동안 특정 시점에 실행되는 함수
const { createApp, ref, onMounted } = Vue
setup() {
onMounted(() => {
console.log('mounted')
})
}