- ํ์ฌ Vue๋ 2๋ฒ์ ์์ 3๋ก ์ด๋์ค .. โ ์์ผ๋ก Vue3๋ก ์งํํ ์์
- ๋ฐ์ดํฐ์ DOM์ด ์ฐ๊ฒฐ๋จ โ ๋ฐ์ํ(๋ฐ์ดํฐ๊ฐ ๋ฐ๋๋ฉด ํ๋ฉด์ด ๋ฐ๋๋ค)
- ์ต์ ๋ฒ์ ์์ ํ๊ธฐ - CDN ๋ฐฉ๋ฒ
- index.html์์ head์
<script src=โhttps://unpkg.com/vue@nextโ></script>
์ฝ๋ ์ถ๊ฐ - CDN ๋ฐฉ๋ฒ์ ์ ์ญ Vue ๊ฐ์ฒด๋ฅผ ํตํด ์ ์ญ API์ ํจ์(ex. createApp)์ ์ ๊ทผํ ์ ์๋ค
Vue.createApp(์ต์ ).mount(ํ๊ฒ์ ๋ ํฐ)
- vue ๋ฐฉ์์ผ๋ก ํ๊ฒ ์ ๋ ํฐ์ ๋ ๋๋ง ์ํค๊ณ , ์ปดํฌ๋ํธ ์ธ์คํด์ค๋ฅผ ๋ฐํ
- ์ต์ ๊ฐ์ฒด์ data๋ฉ์๋, mouted๋ฉ์๋, methods ์์ฑ, components ์์ฑ ๋ฑ์ด ์ฌ ์ ์๋ค
- ์์ฑ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ธ์คํด์ค์์ data๋ฅผ ์์ฑ์ผ๋ก ์ ๊ทผํ ์ ์๋ค(ex. vm.counter)
data
๋ฉ์๋๊ฐ ๋ฆฌํดํ๋ ๊ฐ์ฒด ๋ฉค๋ฒ๋ก ๋ณ์๋ฅผ ์ง์ ํ ์ ์๋ค
mounted
ํ ์ผ๋ก ๋ง์ดํธ ๋ ๋(๋ ๋๋ง ๋ ๋) ์คํ๋์ด์ผ ํ๋ ๋ก์ง์ ์ง์ ํ ์ ์๋ค(์ธ์คํด์ค ๋ฐํ)
- vue.js์์ ์ ์ธํ ๋ณ์ ๋ฑ์ html์์ ์ด์ค ์ค๊ดํธ(
{{ }}
) ์์์ ์ฌ์ฉํด์ ๋ณด๊ฐ ์ฒ๋ฆฌ ํ ์ ์๋ค
... <script src="https://unpkg.com/vue@next"></script> //vue ์์ํ๊ธฐ </head> <body> <div id="app"> {{counter}} //vue์์ ์ ์ธ๋ data ์ฌ์ฉํ๊ธฐ </div> <script> const app = { //์ปดํฌ๋ํธ data() { //๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ ๋ฉ์๋ return { //๊ฐ์ฒด ํํ๋ก ๋ฐํ counter:0 } }, mounted() { //mount๋๋ฉด ์คํ๋๋ ๋ฉ์๋(์ ํ) setInterval(()=> { this.counter += 1 //ํ์ดํ ํจ์์ฌ์ผ ํจ -> this๋ ๋ณ์ app์ ๊ฐ๋ฆฌํด }, 1000) } } const vm = Vue.createApp(app) //vue ์ ํ๋ฆฌ์ผ์ด์ ์์ฑ .mount('#app') //html ์์์ ์ฐ๊ฒฐํ๊ธฐ(์ฒด์ด๋ ๋ฐฉ์) => ์ธ์คํด์ค ์์ฑ </script> </body>
๋๋ ํฐ๋ธ (v-)
- ์
๋ ํฐ ์์ฑ์
v-bind:
๋ฅผ ๋ถ์ฌ์ ๊ฐ์ ๋ฐ์ดํฐ๋ก ์ทจ๊ธํ ์ ์๋ค. - ๋ถ์ผ๋ฉด ๊ฐ์ js๋ก ์ฝํ ์ ์๋ค
- vue์ data ๋ฉ์๋์ ์ ์ธ๋ ๋ณ์๋ฅผ ์ค๊ดํธ ๋๋ฒ ํ์ ์์ด ๋ฐ๋ก ์ธ ์ ์์
- ex) v-bind:class=โ{ ํด๋์ค๋ช : true }โ์ด๋ฉด ํด๋น ํด๋์ค๋ช ์ด ์ ์ฉ ๋๋ค
ex2) v-bind:fruit-name=โnameโ
- ์
๋ ํฐ ์์ฑ์
v-on:
์ผ๋ก method ์ญ์ ๋ถ์ผ ์ ์๋ค - ์์, vue.js์์
methods
๋ผ๋ ์์ฑ์ผ๋ก ๋ฉ์๋๋ค์ ์ ์ธํ ์ ์๋ค - ์ด๋ ๊ฒ ์ ์ธํ ๋ฉ์๋๋ค์
v-on:์์ค์ด๋ฒคํธ์ด๋ฆ=โ๋ฉ์๋์ด๋ฆโ
๋ฑ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค - ex) v-on:click, v-on:scroll, v-on:dragstart
<body> <style> .orange{ //orange ํด๋์ค์ ์คํ์ผ ์ ์ฉ color: orange } </style> <div id="app"> <div v-bind:class="{ orange: active }">{{counter}}</div> //active ๊ฐ์ ๋ฐ๋ผ orange ํด๋์ค๋ช ์ด ์ถ๊ฐ <button v-on:click="increase">Click me!</button> //button์ click ํจ์ ๊ฐ์ผ๋ก increase ๋ฉ์๋ </div> <script> const app = { data() { return { counter: 0, active: true //data์ ๋ฆฌํด ๊ฐ์ active ์์ฑ ์ถ๊ฐ } }, methods: { //methods๋ผ๋ ์์ฑ์์ ๋ฉ์๋๋ฅผ ์ ์ธ ๊ฐ๋ฅ increase() { //counter๋ฅผ 1 ์ฆ๊ฐํ๋ ๋ฉ์๋ this.counter += 1 } } } const vm = Vue.createApp(app).mount('#app') </script> </body>
v-if="๊ฐ"
โ ๊ฐ์ด true์ด๋ฉด ํด๋น ์ ๋ ํฐ ์์๋ฅผ ์ถ๋ ฅํ๋ค
<div id="app"> <div v-if="active">์ง์</div> //๋๋ ํฐ๋ธ <button v-on:click="toggle">Click me!</button> </div> <script> const app = { data() { return { active: true } }, methods: { toggle() { this.active = !this.active } } } const vm = Vue.createApp(app).mount('#app') </script>
v-for=โ์์ in ๋ฐฐ์ด๋ฑโ
ํํ๋ก ๋ฐฐ์ด ๋ฑ์ ๋ฐ๋ณต๋ฌธ์ ๋๋ฆฐ ํ์, ํด๋น ํ๊ทธ์ ์์์์๋{{์์}}
๋ก, ์์ ์์ฑ ๋ฑ์ ๊ฐ์ผ๋ก๋โ์์โ
์์๋ฅผ ์ธ ์ ์๋ค- ๋ฐฐ์ด์๋ script์์ data๋ก ์ ์ธํ ๊ฒ์ด ์ค๊ณ , ์์์ ์ด๋ฆ์ ์๋ฌด๊ฑฐ๋ ์ฌ ์ ์๋ค
<div id="app"> <ul v-for="fruit in fruits"> <li> {{fruit}} </li> </ul> </div> <script> const app = { data() { return { fruits: ['apple', 'banana', 'kiwi'] } } } const vm = Vue.createApp(app).mount('#app') </script>
- v-model ๋๋ ํฐ๋ธ : ์์์ ๋ํ ์ ๋ ฅ๊ณผ ์ฑ ์ํ๋ฅผ ์๋ฐฉํฅ์ผ๋ก ๋ฐ์ธ๋ฉ
- ex) input ์์์ ๊ฐ์ผ๋ก ๋ณ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์์ ๋, ๋ฐ์ดํฐ๋ฅผ ์์ ํ๋ฉด input ์์์ ๊ฐ์ด ๊ฐ์ด ์์ ๋๊ณ ์ญ์ผ๋ก๋ ์ํฅ์ ๋ผ์นจ
์ปดํฌ๋ํธ
- App์ ๊ฐ์ฒด ๋ฉค๋ฒ๋ก components๋ฅผ ๋ ์ ์๊ณ , ๊ฐ์ ๊ฐ์ฒด ํํ๋ก ๋ฐ๊ณ ์ปดํฌ๋ํธ๋ฅผ ๋ฉค๋ฒ๋ก ๋๋ค
- ์ปดํฌ๋ํธ ๊ฐ์ฒด: ๋ฉค๋ฒ template์์ renderํ๊ณ ์ถ์ html์ฝ๋, ๋ฉค๋ฒ props์์ ์ ๋ฌ๋ฐ์ props
- script์์ ์ ์ธ๋ components๋ฅผ ๊บฝ์ ๋ก html ์ฝ๋์์ ์ธ ์ ์๋ค
- ์ฃผ์) js์์๋ components์ props ์ด๋ฆ์ ์นด๋ฉ ์ผ์ด์ค๋ก, html์์๋ ๋์ ์ผ์ด์ค๋ก ๋ฐ์. (๋ทฐ๊ฐ ์์์ ๋ณํํ์ฌ ๋งค์นญ ํด์ค)
<div id="app"> <ul v-for="name in fruits"> <fruit-item v-bind:fruit-name="name"></fruit-item> </ul> </div> <script> const FruitItem = { props: ['fruitName'], template: "<li>{{fruitName}}</li>" } const app = { components: { FruitItem }, data() { return { fruits: ['apple', 'banana', 'kiwi'] } } } const vm = Vue.createApp(app).mount('#app') </script>
- live server ํ์ฅ ํ๋ก๊ทธ๋จ์ผ๋ก ๋ฐ๋ก ์๋ฒ๋ฅผ ์์ํ ์ ์๋ค.(์ ์ฅ ์ ์๋ ์๋ก๊ณ ์นจ)
- ์ฝ๋ ์ฐํด๋ฆญ ํ Open with live server ํด๋ฆญ
- script์ ์ ์ญ ๋ณ์์ ์ด๋ฆ์ ๊ฐ๋ฐ์ ๋๊ตฌ ์ฝ์์ ์ ๋ ฅํ๋ฉด, ํด๋น ๋ณ์ ์ ๋ณด๋ฅผ ํ์ธํ ์ ์๋ค
- counter๋ app.counter๋ก ์ ๊ทผ, ๋ณ๊ฒฝํ ์ ์๋ค

- ํจ์์ ๋ฐ๋ฅธ this
- ์ผ๋ฐ function ํจ์ โ ํจ์๊ฐ ํธ์ถ๋๋ ์์น์์ ์ ์
- ํ์ดํ ํจ์ โ ํจ์๊ฐ ์ ์ธ๋๋ ์์น์์ ์ ์
- ๋ณดํต์ ํ์ดํ ํจ์๋ฅผ ์จ์ ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ํจ