๐Template Syntax
|Template Syntax
- ๋ ๋๋ง ๋ DOM์ ๊ธฐ๋ณธ Vue ์ธ์คํด์ค์ ๋ฐ์ดํฐ์ ์ ์ธ์ ์ผ๋ก ๋ฐ์ธ๋ฉํ ์ ์๋ HTML ๊ธฐ๋ฐ ํ
ํ๋ฆฟ ๊ตฌ๋ฌธ์ ์ฌ์ฉ
- Interpolation
- Directive
|Interpolation(๋ณด๊ฐ๋ฒ)
1.Text
<span>๋ฉ์์ง: {{message}} </span>
2. Raw HTML
<span v-html="rawHtml"></span>
3.Attributes
<div v-bind:id="dynamicID"></div>
4.JSํํ์
{{number+1}}
{{message.split('').reverse().join('')}}
|Directive(๋๋ ํฐ๋ธ)
- v- ์ ๋์ฌ๊ฐ ์๋ ํน์ ์์ฑ
- ์์ฑ ๊ฐ์ ๋จ์ผ JS ํํ์์ด ๋จ (v-for๋ ์์ธ)
- ํํ์์ ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋ ๋ฐ์์ ์ผ๋ก DOM์ ์ ์ฉํ๋ ์ญํ ์ ํจ
- ์ ๋ฌ์ธ์(Arguments)
- : (์ฝ๋ก )์ ํตํด ์ ๋ฌ์ธ์๋ฅผ ๋ฐ์ ์๋ ์์
<a v-bind:href="url">...</a> <a v-on:click="doSomething">...</a>
- ์์์ด (Modifiers)
- . (์ )์ผ๋ก ํ์๋๋ ํน์ ์ ๋ฏธ์ฌ
- directive๋ฅผ ํน๋ณํ ๋ฐฉ๋ฒ์ผ๋ก ๋ฐ์ธ๋ฉ ํด์ผ ํจ์ ๋ํ๋
<from v-on:submit.prevent="onSubmit">...</form>
|v-text —→๊ณต๊ฒฉ์ ์ทจ์ฝ.
- ์๋ฆฌ๋จผํธ์ textContent๋ฅผ ์ ๋ฐ์ดํธ
- ๋ด๋ถ์ ์ผ๋ก interpolation ๋ฌธ๋ฒ์ด v-text๋ก ์ปดํ์ผ ๋จ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<p>{{ message }}</p>
<p v-text="message"></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
message:'Hello'
}
})
</script>
</body>
</html>
|v-html —→์ ๋ ์ฌ์ฉ ๊ธ์ง
- ์๋ฆฌ๋จผํธ์ innerHTML์ ์
๋ฐ์ดํธ
- XSS ๊ณต๊ฒฉ์ ์ทจ์ฝํ ์ ์์
- ์์๋ก ์ฌ์ฉ์๋ก๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๋ด์ฉ์ v-html์ ‘์ ๋’ ์ฌ์ฉ ๊ธ์ง
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<p>{{ message }}</p>
<p v-text="message"></p>
<p v-html="message"></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
message: '<strong>๊ฐํ๋ท</strong>'
}
})
</script>
</body>
</html>
|v-show — CSS์์ฑ๋ง ๊ฑด๋๋ฆด ๋ ์ฌ์ฉ. ๋๋๋ก v-if๋ฅผ ์ฌ์ฉํ ๊ฒ
- ์กฐ๊ฑด๋ถ ๋ ๋๋ง ์ค ํ๋
- ์์๋ ํญ์ ๋ ๋๋ง ๋๊ณ DOM์ ๋จ์์์
- ๋จ์ํ ์๋ฆฌ๋จผํธ์ display CSS ์์ฑ์ ํ ๊ธํ๋ ๊ฒ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<p v-show="isTrue">TRUE</p>
<p v-show="isFalse">FALSE</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
isTrue: true,
isFalse: false,
}
})
</script>
</body>
</html>
|v-if, v-else-if, v-else
- ์กฐ๊ฑด๋ถ ๋ ๋๋ง ์ค ํ๋
- ์กฐ๊ฑด์ ๋ฐ๋ผ ์์๋ฅผ ๋ ๋๋ง
- directive์ ํํ์์ด true์ผ ๋๋ง ๋ ๋๋ง
- ์๋ฆฌ๋จผํธ ๋ฐ ํฌํจ๋ directive๋ ํ ๊ธํ๋ ๋์ ์ญ์ ๋๊ณ ๋ค์ ์์ฑ๋จ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<P v-if="seen">seen is True</P>
<p v-if="myType === 'A' ">A</p>
<p v-else-if="myType === 'B' ">B</p>
<p v-else-if="myType === 'C' ">C</p>
<p v-else>NOT A/B/C</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
seen:false,
myType: 'A',
}
})
</script>
</body>
</html>
|v-show์ v-if
v-show (Expensive initial load, cheap toggle)
- CSS display ์์ฑ์ hidden์ผ๋ก ๋ง๋ค์ด ํ ๊ธ
- ์ค์ ๋ก ๋ ๋๋ง์ ๋์ง๋ง ๋์์ ๋ณด์ด์ง ์๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ฑ ํ ๋ฒ๋ง ๋ ๋๋ง์ด ๋๋ ๊ฒฝ์ฐ๋ผ๋ฉดv-if์ ๋นํด ์๋์ ์ผ๋ก ๋ ๋๋ง ๋น์ฉ์ด ๋์
- ํ์ง๋ง, ์์ฃผ ๋ณ๊ฒฝ๋๋ ์์๋ผ๋ฉด ํ ๋ฒ ๋ ๋๋ง ๋ ์ดํ๋ถํฐ๋ ๋ณด์ฌ์ฃผ๋์ง์ ๋ํ ์ฌ๋ถ๋ง ํ๋จํ๋ฉด ๋๊ธฐ ๋๋ฌธ์ ํ ๊ธ ๋น์ฉ์ด ์ ์
v-if (Cheap initial load, expensive toggle)
- ์ ๋ฌ์ธ์๊ฐ false์ธ ๊ฒฝ์ฐ ๋ ๋๋ง ๋์ง ์์
- ํ๋ฉด์์ ๋ณด์ด์ง ์์ ๋ฟ๋ง ์๋๋ผ ๋ ๋๋ง ์์ฒด๊ฐ ๋์ง ์๊ธฐ ๋๋ฌธ์ ๋ ๋๋ง ๋น์ฉ์ด ๋ฎ์
- ํ์ง๋ง, ์์ฃผ ๋ณ๊ฒฝ๋๋ ์์์ ๊ฒฝ์ฐ ๋ค์ ๋ ๋๋ง ํด์ผ ํ๋ฏ๋ก ๋น์ฉ์ด ์ฆ๊ฐํ ์ ์์
|v-for
<body>
<div id="app">
<h2>String</h2>
<div v-for="char in myStr">
{{ char }}
</div>
<h2>Array</h2>
<div v-for="fruit in fruits">
{{ fruit }}
</div>
<div v-for="(fruit, idx) in fruits" :key = "idx">
{{ idx }} => {{ fruit }}
</div>
<div v-for="todo in todos" :key ="`todo-${todo.id}`">
<p>{{ todo.title }} => {{ todo.completed }}</p>
</div>
<h2>Object</h2>
<div v-for="value in myObj">
{{ value }}
</div>
<div v-for="(value, key) in myObj">
{{ key }} => {{ value }}
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
myStr: 'Hello World!',
fruits: ['apple', 'banana', 'coconut'],
todos: [
{ id: 1, title: 'todo1', completed: true },
{ id: 2, title: 'todo2', completed: false },
{ id: 3, title: 'todo3', completed: true },
],
myObj: {
name: 'kim',
age: 100,
}
}
})
</script>
</body>
</html>
- ์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์๋ฆฌ๋จผํธ ๋๋ ํ ํ๋ฆฟ ๋ธ๋ก์ ์ฌ๋ฌ ๋ฒ ๋ ๋๋ง
- item in items ๊ตฌ๋ฌธ ์ฌ์ฉ
- item ์์น์ ๋ณ์๋ฅผ ๊ฐ ์์์์ ์ฌ์ฉํ ์ ์์
- ๊ฐ์ฒด์ ๊ฒฝ์ฐ๋ key
- v-for ์ฌ์ฉ ์ ๋ฐ๋์ key ์์ฑ์ ๊ฐ ์์์ ์์ฑ
- v-if์ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒฝ์ฐ v-for๊ฐ ์ฐ์ ์์๊ฐ ๋ ๋์
- ๋จ, ๊ฐ๋ฅํ๋ฉด v-if์ v-for๋ฅผ ๋์์ ์ฌ์ฉํ์ง ๋ง ๊ฒ
|v-on
<body>
<div id="app">
<!-- ๋ฉ์๋ ํธ๋ค๋ฌ -->
<button v-on:click="alertHello">Button</button>
<button @click="alertHello">Button</button>
<!-- ๊ธฐ๋ณธ ๋์ ๋ฐฉ์ง -->
<form action=""@submit.prevent="alertHello">
<button>GoGo</button>
</form>
<!-- ํค ๋ณ์นญ์ ์ด์ฉํ ํค ์
๋ ฅ ์์์ด -->
<!-- <input type="text" @keyup.space="log"> -->
<input type="text" @keyup.enter="log">
<!-- cdํจ์์์ ํน์๋ฌธ๋ฒ ()-->
<!-- log๋ฅผ ์คํํ ๊ฑด๋ฐ, 1๋ฒ ์ธ์๋ก 'asdf'๋ฅผ ๋๊ธฐ๊ณ ์ถ๋ค. -->
<input type="text" @keyup.enter="log('asdf')">
<p>{{ message }}</p>
<button @click="changeMessage">change message</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
//๊ฐ๋ค์ ๋ชจ์
data: {
message: 'Hello Vue',
},
//ํ๋๋ค์ ๋ชจ์
methods: {
alertHello: function(){
alert('hello')
},
log: function(something){
console.log(something)
},
changeMessage(){
this.message = 'new message!'
}
}
})
</script>
</body>
- ์๋ฆฌ๋จผํธ์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ฐ๊ฒฐ
- ์ด๋ฒคํธ ์ ํ์ ์ ๋ฌ์ธ์๋ก ํ์ํจ
- ํน์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋, ์ฃผ์ด์ง ์ฝ๋๊ฐ ์คํ ๋จ
- ์ฝ์ด (Shorthand)
- @
- v-on:click → @click
|v-bind
<head>
<style>
.active {
color: red;
}
.my-background-color {
background-color: yellow;
}
</style>
</head>
<body>
<div id="app">
<!-- ์์ฑ ๋ฐ์ธ๋ฉ -->
<img v-bind:src="imageSrc" :alt="altMsg">
<img :src="imageSrc" alt="altMsg">
<hr>
<!-- ํด๋์ค ๋ฐ์ธ๋ฉ -->
<div :class="{ active: isRed }">
ํด๋์ค ๋ฐ์ธ๋ฉ
</div>
<hr>
<h3 :class="[activeRed, myBackgroud]">
hello Vue
</h3>
<!-- ์คํ์ผ ๋ฐ์ธ๋ฉ -->
<p :style="{ fontSize: fontSize + 'px' }">
this is paragraph
</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
fontSize: 16,
altMsg: 'this is image',
imageSrc: 'https://picsum.photos/200/300/',
isRed: true,
activeRed: 'active',
myBackgroud: 'my-background-color',
}
})
</script>
</body>
- HTML ์์์ ์์ฑ์ Vue์ ์ํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ผ๋ก ํ ๋น
- Object ํํ๋ก ์ฌ์ฉํ๋ฉด value๊ฐ true์ธ key๊ฐ class ๋ฐ์ธ๋ฉ ๊ฐ์ผ๋ก ํ ๋น
- ์ฝ์ด (Shorthand)
- : (์ฝ๋ก )
- v-bind:href → :href
|v-model — HTML input์์์ data๋ฅผ ํ ๋ชธ์ผ๋ก ๋ง๋ ๋ค!
<body>
<div id="app">
<h2>Input -> Data ๋จ๋ฐฉํฅ</h2>
<p>{{ msg1 }}</p>
<input type="text" @input="onInputChange">
<hr>
<h2>Input <-> Data ์๋ฐฉํฅ</h2>
<P>{{ msg2 }}</P>
<input type="text" v-model="msg2">
<hr>
์ณ!<input id="box" type="checkbox" v-model="checked">
<label for="box">{{ checked }}</label>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
msg1:'111',
msg2:'222',
checked: true,
},
methods:{
onInputChange(event){
this.msg1= event.target.value
}
},
})
</script>
</body>
- HTML form ์์์ ๊ฐ๊ณผ data๋ฅผ ์๋ฐฉํฅ ๋ฐ์ธ๋ฉ
- ์์์ด
- .lazy
- input ๋์ change ์ด๋ฒคํธ ์ดํ์ ๋๊ธฐํ
- .number
- ๋ฌธ์์ด์ ์ซ์๋ก ๋ณ๊ฒฝ
- .trim
- ์ ๋ ฅ์ ๋ํ trim์ ์งํ
- .lazy
|Options/Data = ‘computed'
<body>
<div id="app">
<input type="text" v-model="r">
<p>{{r}}</p>
<p>{{area}}</p>
<p>{{perim}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
r: 2,
},
computed:{
area: function(){
return this.r**2*3.14
},
perim: function(){
return this.r*2*3.14
}
}
})
</script>
</body>
- ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ ๊ณ์ฐ๋ ์์ฑ
- ํจ์์ ํํ๋ก ์ ์ํ์ง๋ง ํจ์๊ฐ ์๋ ํจ์์ ๋ฐํ ๊ฐ์ด ๋ฐ์ธ๋ฉ ๋จ
- ์ข ์๋ ๋ฐ์ดํฐ์ ๋ฐ๋ผ ์ ์ฅ(์บ์ฑ)๋จ
- ์ข ์๋ ๋ฐ์ดํฐ๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง ํจ์๋ฅผ ์คํ
- ์ฆ, ์ด๋ค ๋ฐ์ดํฐ์๋ ์์กดํ์ง ์๋ computed ์์ฑ์ ๊ฒฝ์ฐ ์ ๋๋ก ์ ๋ฐ์ดํธ ๋์ง ์์
- ๋ฐ๋์ ๋ฐํ ๊ฐ์ด ์์ด์ผ ํจ
|computed & methods
- computed ์์ฑ ๋์ methods์ ํจ์๋ฅผ ์ ์ํ ์๋ ์์
- ์ต์ข ๊ฒฐ๊ณผ์ ๋ํด ๋ ๊ฐ์ง ์ ๊ทผ ๋ฐฉ์์ ์๋ก ๋์ผ
- ์ฐจ์ด์ ์ computed ์์ฑ์ ์ข ์ ๋์์ ๋ฐ๋ผ ์ ์ฅ(์บ์ฑ) ๋จ
- ์ฆ, computed๋ ์ข ์๋ ๋์์ด ๋ณ๊ฒฝ๋์ง ์๋ ํ computed์ ์์ฑ๋ ํจ์๋ฅผ ์ฌ๋ฌ ๋ฒ ํธ์ถํด๋ ๊ณ์ฐ์ ๋ค์ ํ์ง ์๊ณ ๊ณ์ฐ๋์ด ์๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
- ์ด์ ๋นํด methods๋ฅผ ํธ์ถํ๋ฉด ๋ ๋๋ง์ ๋ค์ ํ ๋๋ง๋ค ํญ์ ํจ์๋ฅผ ์คํ
|Options/Data - ‘watch’ —→๋๋๋ก ์ฐ์ง ๋ง๊ฒ.... computed๋ฅผ ์ฐ์
<body>
<div id="app">
<p>{{ num }}</p>
<button @click="num += 1">add 1</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
num: 2,
},
watch: {
num: function () {
console.log(`${this.num}์ด ๋ณ๊ฒฝ๋์์ต๋๋ค.`)
}
},
})
</script>
</body>
- ๋ฐ์ดํฐ๋ฅผ ๊ฐ์
- ๋ฐ์ดํฐ์ ๋ณํ๊ฐ ์ผ์ด๋ฌ์ ๋ ์คํ๋๋ ํจ์
|computed & watch
computed์ watch๋ ์ด๋ค ๊ฒ์ด ๋ ์ฐ์ํ ๊ฒ์ด ์๋ ์ฌ์ฉํ๋ ๋ชฉ์ ๊ณผ ์ํฉ์ด ๋ค๋ฆ
computed
- ํน์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ์ ์ผ๋ก ์ฌ์ฉ/๊ฐ๊ณตํ์ฌ ๋ค๋ฅธ ๊ฐ์ผ๋ก ๋ง๋ค ๋ ์ฌ์ฉ
- ์์ฑ์ ๊ณ์ฐํด์ผ ํ๋ ๋ชฉํ ๋ฐ์ดํฐ๋ฅผ ์ ์ํ๋ ๋ฐฉ์์ผ๋ก ์ํํธ์จ์ด ๊ณตํ์์ ์ด์ผ๊ธฐํ๋ ‘์ ์ธํ ํ๋ก๊ทธ๋๋ฐ’ ๋ฐฉ์
- “ํน์ ๊ฐ์ด ๋ณ๋ํ๋ฉด ํด๋น ๊ฐ์ ๋ค์ ๊ณ์ฐํด์ ๋ณด์ฌ์ค๋ค.”
watch
- ํน์ ๋ฐ์ดํฐ์ ๋ณํ ์ํฉ์ ๋ง์ถฐ ๋ค๋ฅธ data ๋ฑ์ด ๋ฐ๋์ด์ผ ํ ๋ ์ฃผ๋ก ์ฌ์ฉ
- ๊ฐ์ํ ๋ฐ์ดํฐ๋ฅผ ์ง์ ํ๊ณ ๊ทธ ๋ฐ์ดํฐ๊ฐ ๋ฐ๋๋ฉด ํน์ ํจ์๋ฅผ ์คํํ๋ ๋ฐฉ์
- ์ํํธ์จ์ด ๊ณตํ์์ ์ด์ผ๊ธฐํ๋ ‘๋ช ๋ นํ ํ๋ก๊ทธ๋๋ฐ’ ๋ฐฉ์
- “ํน์ ๊ฐ์ด ๋ณ๋ํ๋ฉด ๋ค๋ฅธ ์์ ์ ํ๋ค.”
- ํน์ ๋์์ด ๋ณ๊ฒฝ๋์์ ๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํ์ํค๊ธฐ ์ํ ํธ๋ฆฌ๊ฑฐ
|์ ์ธํ & ๋ช ๋ นํ
- ์ ์ธํ ํ๋ก๊ทธ๋๋ฐ
- “๊ณ์ฐํด์ผ ํ๋ ๋ชฉํ ๋ฐ์ดํฐ๋ฅผ ์ ์” (computed)
- ๋ช
๋ นํ ํ๋ก๊ทธ๋๋ฐ
- “๋ฐ์ดํฐ๊ฐ ๋ฐ๋๋ฉด ํน์ ํจ์๋ฅผ ์คํํด!” (watch)
|Options / Assets - ‘filter’
<body>
<div id="app">
{{ numbers | getOddNumbers | getUnderTen}}
<hr>
{{ getOddAndUnderTen }}
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
},
filters: {
getOddNumbers(array) {
return array.filter(num => num % 2)
},
getUnderTen(array) {
return array.filter(num=>num<10)
}
},
// w/computed//
computed: {
getOddAndUnderTen() {
return this.numbers.filter(num=> num%2 && num<10)
}
}
})
</script>
</body>
- ํ ์คํธ ํ์ํ๋ฅผ ์ ์ฉํ ์ ์๋ ํํฐ
- interpolation ํน์ v-bind๋ฅผ ์ด์ฉํ ๋ ์ฌ์ฉ ๊ฐ๋ฅ
- ํํฐ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํํ์ ๋ง์ง๋ง์ “|” (ํ์ดํ)์ ํจ๊ป ์ถ๊ฐ๋์ด์ผ ํจ
- ์ด์ด์ ์ฌ์ฉ(chaining) ๊ฐ๋ฅ
'Development > Vue.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Vue.js - Vue CLI (0) | 2022.05.09 |
---|---|
Vue.js - SFC(Single File Component) (0) | 2022.05.09 |
Vue.js - Lifecycle Hooks (0) | 2022.05.07 |
Vue.js - Vue Instance (0) | 2022.05.07 |
Vue.js - SPA, CSR,SSR,SEO,Vue.js์ ๊ฐ๋ ๊ณผ ์ญํ , MVVM (0) | 2022.05.07 |
๋๊ธ