<p>Message: {{ msg }}</p>
<div v-html="rawHtml"></div>
const rawHtmal = ref('<span style="color:red">This should be red.</span>')
# ----------
<div>
<span style="color:red">This should be red.</span>
</div>
<div v-bind:id="dynamicId"></div>
const dynamicId = ref('my-id')
# ----------
<div id="my-id"></div>
{{ number + 1 }}
{{ ok ? 'YES' : 'NO' }}
{{ message.split('').reverse().join('') }}
<div :id="`list-${id}`"></div>
<!-- 표현식이 아닌 선언식 -->
{{ const number = 1 }}
<!-- 제어문은 삼항 표현식을 사용해야 함 -->
{{ if (ok) { return message } }}
<p v-if="seen">Hi There</p>
<a> 요소의 href 속성값을 myUrl 값에 바인딩하도록 하는 v-bind의 인자<a v-bind:href="myUlr">Link</a><button v-on:click="doSomething">Button</button><form @submit.prevent="onSubmit">...</form><img v-bind:src="imageSrc">
<a v-bind:href="myUrl">Move to url</a>
<img :src="imageSrc">
<a :href="myUrl">Move to url</a>
<button :[key]="myValue"></button>
<img :src="imageSrc">
<a :href="myUrl">Move to Url</a>
<p :[dynamicattr]="dynamicValue">...</p>
const app = createApp({
setup() {
const imageSrc = ref('https://picsum.photos/200')
const myUrl = ref('https://www.google.co.kr/')
const dynamicattr = ref('title')
const dynamicValue = ref('Hello Vue.js')
return {
imageSrc,
myUrl,
dynamicattr,
dynamicValue,
}
},
})
app.mount('#app')
1.1 Binding to Objects
js
const isActive = ref(false)
html
<div :class="{ active: isActive }">Text</div>
1.2 Binding to Arrays
const isActive = ref(false)
const hasInfo = ref(true)
<div class="static" :class="{ active: isActive, 'text-primary': hasInfo }">Text</div>
div class="static text-primary">Text</div>
2.1 Binding to Objects
const activeColor = ref('crimson')
const fontSize = ref(50)
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }">Text</div>
<div :style="color: crimson; font-size: 50px;">Text</div>
<div :style="{ 'font-size': fontSize + 'px' }">Text</div>
<div style="font-size: 50px;">Text</div>
2.2 Bindings to Arrays
v-on:event="handler"
@event="handler"const count = ref(0)
<button @click="count++">Add 1</button>
<p>Count: {{ count }}</p>
js
const name = ref('Alice')
const myFunc = function (event) {
console.log(event)
console.log(event.currentTarget)
console.log(`Hello ${name.value}!`)
}
html
<button @click="muFunc">Hello</button>
console

js
const myFunc = function (event) {
console.log(event)
console.log(event.currentTarget)
console.log(`Hello ${name.value})
}
const greeting = function (message) {
console.log(message)
}
<button @click="greeting('hello')">Say Hello</button>
<button @click="greeting('bye')">Say bye</button>
const warning = function (message, event) {
console.log(message)
console.log(event)
}
<button @click="warning('경고입니다', $event)">Submit</button>
메서드는 DOM 이벤트에 대한 처리보다는 데이터에 관한 논리를 작성하는 것에 집중할 것
<form @submit.prevent="onSubmit">...</form>
<a @click.stop.prevent="onLink">...</a>
Vue는 키보드 이벤트를 수신할 때 특정 키에 관한 별도 modifiers를 사용할 수 있음
예시
<input @keyup.enter="onSubmit">form을 처리할 때 사용자가 input에 입력하는 값을 실시간으로 JavaScript 상태에 동기화해야 하는 경우 (양방향 바인딩)
양방향 바인딩 방법
const inputText1 = ref('')
const onInput = function (event) {
inputText1.value = event.currentTarget.value
}
<p>{{ inputText1 }}</p>
<input :value="inputText1" @input="onInput">
const inputText2 = ref('')
<p>{{ inputText2 }}</p>
<input v-model="inputText2">
IME가 필요한 언어(한국어, 중국어, 일본어 등)의 경우 v-model이 제대로 업데이트되지 않음
해당 언어에 대해 올바르게 응답하려면 v-bind와 v-on 방법을 사용해야 함
const checked = ref(false)
<input type="checkbox" id="checkbox" v-model="checked">
<label for="checkbox">{{ checked }}</label>
const checkedNames = ref([])
<div>Checked names: {{ checkedNames }}</div>
<input type="checkbox" id="alice" value="Alice" v-model="checkedNames">
<labe for="alice">Alice</label>
<input type="checkbox" id="bella" value="Bella" v-model="checkedNames">
<labe for="bella">Bella</label>
const selected = ref('')
<div> Selected: {{ selected }}</div>
<select> v-model="selected">
<option disabled value="">Please select one</option>
<option>Alice</option>
<option>Bella</option>
<option>Cathy</option>
</select>
IME가 동작하는 방식과 Vue의 양방향 바인딩(v-model) 동작 방식이 상충하기 때문에 한국어 입력시 예상대로 동작하지 않았던 것