Vue.js Starter - Data Binding
Vue.js ํ๋ก์ ํธ ํฌ์ ์ผ์ฃผ์ผ ์ - Part 2
3. Vue Router ๐ฉโ๐ป
1. Installation
vue create
๋ฅผ ํตํด ํ๋ก์ ํธ ์์ฑํ ๋ Router
๋ฅผ ํ๋ก์ ํธ์ ์ค์ ํ์ง ์์๋ค๋ฉด ๋ค์ ๋ช
๋ น์ด๋ฅผ ํตํด ์ค์นํ๋ค.
vue add router
@vue/cli-plugin-router
๊ฐ ์ค์น๋๋ฉฐ, src ์๋ router
, view
๋๋ ํ ๋ฆฌ ๋ฐ ํ์ผ์ด ์์ฑ๋๋ค.
2. Run App with Vue Router
1 ) main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
createApp(App).use(router).mount('#app')
node_modules
์์vue
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก๋ถํฐcreateApp
์ ๊ฐ์ ธ์จ๋ค../App.vue
๋ฅผApp
์ผ๋ก ๊ฐ์ ธ์จ๋ค(์๋์ผ๋ก default export ๊ฐ ์ ์ฉ๋๋ค)../router/index.js
๋ฅผrouter
๋ก ๊ฐ์ ธ์จ๋ค.
(ํ์ผ๋ช ์ด index ์ผ ๊ฒฝ์ฐ๋ ์๋ต ๊ฐ๋ฅํ๋ฏ๋ก โ./routerโ ๋ก ์ ์ ์ ์๋ค.
index.js
ํ์ผ์ ๋ณด๋ฉด ์ ์ ์๋ฏ์ดexport default router
๋ฅผ ํ๊ณ ์๊ธฐ ๋๋ฌธ์import { router }
๋ก ์ ์ง ์๊ณ{ }
๋ก ์ ๋๋ค).
index.html
document ๋ฅผ ๋ฐ์ ํ ์ฑ์ดVue
๋ฅผ ํตํด ์์๋๋Entry Point
๋ค.
2 ) App.vue
<template>
<nav>
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</nav>
<router-view/>
</template>
-
โ :
Vue
์ ์ํด<a href=""></a>
HTML ์ด ์๋ ์์ฑ๋๋ค. -
: ๋ผ์ฐํฐ์ ์ํด ๋งค์นญ๋ ์ปดํฌ๋ํธ๊ฐ ์ด ๊ณณ์ ๋ ๋๋ง ๋๋ค.
3 ) index.js
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
}
]
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
})
export default router
node_modules
์์vue-router
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก๋ถํฐcreateRouter
๋ฅผ ๊ฐ์ ธ์จ๋ค(createWebHistory
๋ optional).- ๋ผ์ฐํฐ๋ฅผ ์์ฑํ๊ณ , ์ด ๋ผ์ฐํฐ๋ฅผ
default
๋กexport
ํ๋ค.
routes
Object ๋ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌ์ฑ๋๋ค.
- path : ๋ผ์ฐํฐ์
endopoint
- name : ๋ผ์ฐํฐ์ ์ด๋ฆ
- component :
Vue
์ปดํฌ๋ํธ
(component: HomeView
์ฒ๋ผimport HomeView from '../views/HomeView.vue'
๋ฅผ ๋ถ๋ฆฌํ ์๋ ์๊ณ ,
component: () => import('../views/AboutView.vue')
์ ๊ฐ์ด ํด๋ก์ ๋ฅผ ์ด์ฉํ ์๋ ์๋ค.)
3. Lazy Load
Vue CLI 3
๋ถํฐ prefetch
๊ธฐ๋ฅ์ด ์ถ๊ฐ๋์ด ์ปดํฌ๋ํธ๋ฅผ ๋ฏธ๋ฆฌ ๋ก๋ฉํ ์ง, Lazy
๋ก๋ฉํ ์ง๋ฅผ ๊ตฌ๋ถํ ์ ์๋ค. ๊ธฐ๋ณธ๊ฐ์ true
๋ก
๋ณ๋๋ก ์ง์ ํ์ง ์์ผ๋ฉด prefetch
๋ก ์๋๋๋ค.
prefetch
๋ฅผ ์ ์ฉํ๋ฉด, ์ต์ด ์ฑ ๋ก๋ฉ ํ ๋น ๋ฅธ ์๋๋ฅผ ๊ธฐ๋ํ ์ ์๋ ๋ฐ๋ฉด ์ด๊ธฐ ๋ก๋ฉ์ด ๋๋ ค์ง๋ค. ๋ฐ๋ผ์ prefetch
๋ฅผ ์ ์ฉํ ๊ณณ๊ณผ
์ ์ฉํ์ง ์์ ๊ณณ์ ๊ตฌ๋ณํด ์ด๊ธฐ ๋ก๋ฉ ์๋๋ฅผ ๋น ๋ฅด๊ฒ ํ๋ฉด์, ์ผ๋ถ ๋ฌด๊ฑฐ์ด ์ปดํฌ๋ํธ๋ฅผ ์ ์ ํ ๋ถ๋ฆฌํ๋ ๊ฒ์ด ํ์ํ๋ค.
๋ค์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค.
1 ) prefetch true
prefetch
๋ ๋ณ๋๋ก ๋์ง ์์ผ๋ฉด Vue CLI 3
์์๋ ๊ธฐ๋ณธ๊ฐ์ผ๋ก true
๋ฅผ ๊ฐ๋๋ค.
๋ฐ๋ผ์, Lazy
๋ก๋ฉ์ด ํ์ํ ์ปดํฌ๋ํธ import
์ /* webpackChunkName: "about" */
์ฝ๋ฉํธ๋ฅผ ์ถ๊ฐํด ๋ณ๋์ ํ์ผ๋ก ๋น๋ํ
์ ์๋ค. ์ด๊ฒ์ chunk
๋ฅผ ๋ง๋ ๋ค๊ณ ํ๋ฉฐ, ์ด ๊ฒฝ์ฐ about.js
๋ผ๋ ํ์ผ์ ๋ถ๋ฆฌ ๋น๋ํ๋ค.
const Home = () => import(/* webpackChunkName: "home" */ './Home.vue');
const About = () => import(/* webpackChunkName: "about" */ './About.vue');
const Contact = () => import(/* webpackChunkName: "contact" */ './Contact.vue');
2 ) prefetch false
๊ธฐ๋ณธ์ ์ผ๋ก prefetch
๋ฅผ ๊บผ ๋ชจ๋ ์ปดํฌ๋ํธ๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก Lazy
๋ก๋ฉ๋๋๋ก ํ ํ, prefetch
๊ฐ ํ์ํ ๊ณณ์ ๋ช
์ํ ์ ์๋ค.
์ ๋ฐฉ๋ฒ๊ณผ ์ ๋ฐ๋ ๋๋ ๋ฐฉ๋ฒ์ผ๋ก ์ฐ์ vue.config.js
์์ prefetch
๋ฅผ ๋๋๋ก ํ๋ค.
module.exports = {
chainWebpack: config => {
config.plugin.delete('prefetch');
}
}
๊ทธ๋ฆฌ๊ณ prefetch
๊ฐ ํ์ํ ์ปดํฌ๋ํธ import
์ /* webpackPrefetch: true */
์ฝ๋ฉํธ๋ฅผ ์ถ๊ฐํ๋ค.
{
path: '/about',
name: 'about',
component: () => import(/* webpackPrefetch: true */ '../views/AboutView.vue')
}
4. Vue Component ๐ฉโ๐ป
Component Structures
<template>
<div></div>
</template>
<script>
export default {
name: "", // ์ปดํฌ๋ํธ ์ด๋ฆ(ํ์๊ฐ์ ์๋)
components: {}, // ์ธ๋ถ ์ปดํฌ๋ํธ๋ฅผ import
data() { // html ๊ณผ javascript ์์ ์ฌ์ฉํ ๋ฐ์ดํฐ ๋ณ์๋ก `Vue`์ ์ํด ์ปจํธ๋กค
return {}
},
setup() { // (Lifecycle Hooks) Composition API
},
created() { // (Lifecycle Hooks) ์ปดํฌ๋ํธ init ํ
},
mounted() { // (Lifecycle Hooks) ์ปดํฌ๋ํธ initial render ํ
},
unmounted() { // (Lifecycle Hooks) ์ปดํฌ๋ํธ ์ธ๋ง์ดํธ ์
},
methods: {}, // ์ปดํฌ๋ํธ ๋ด์์ ์ฌ์ฉํ ๋ฉ์๋
}
</script>
<style scoped>
</style>
5. Data Binding โv-modelโ ๐ฉโ๐ป
Vue
๋ Anlgular
์ ๋ง์ฐฌ๊ฐ์ง๋ก Two-way data binding
์ ์ง์ํ๋ค.
1. Text Data Binding
{{ someData }} ๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
<template>
<div>{{ someData }}</div>
</template>
<script>
export default {
data() {
return {
someData: `Some Text`
}
},
}
</script>
2. Raw HTML Data Binding
{{ someData }} ๋ก ์์ฑํ ๊ฒฝ์ฐ ๋จ์ ํ
์คํธ๋ก ์ธ์๋๊ธฐ ๋๋ฌธ์ v-html
directive ๋ฅผ ์ด์ฉํด
<div v-html="someData"></div> ๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
<template>
<div v-html="someData"></div>
</template>
<script>
export default {
data() {
return {
someData: `<Some HTML></Some HTML>`
}
},
}
</script>
<div v-html="someData"></div>
๋๋<span v-html="someData"></span>
์ ๊ฐ์ดchildren
์ด ์๋ ๋นelement
๋ฅผ ์์ฑํ๋ค.<div v-html="someData"></div>
์children
์ด export ๋๋ element ๋กoverride
๋๋ค.
Raw HTML Data Binding Examples
<template>
<div v-html="someHtml"></div>
</template>
<script>
export default {
data() {
return {
someHtml: `<p style="color: red;">This is red.</p>`
}
},
}
</script>
3. Form input type="text"
v-model
directive ๋ฅผ ์ด์ฉํด
<input type=โtextโ v-model=โsomeValueโ> ๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
<template>
<input type="text" v-model="someValue">
</template>
<script>
export default {
data() {
return {
someValue: `Some Text`
}
},
}
</script>
4. Form input type="number"
v-model.number
directive ๋ฅผ ์ด์ฉํด
<input type=โtnumberโ v-model=โsomeNumberโ> ๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
<template>
<input type="number" v-model.number="someNumber">
</template>
<script>
export default {
data() {
return {
someNumber: 0
}
},
}
</script>
5. Form textarea
<textarea>{{ someText }}</textarea>
์ผ ๊ฒ ๊ฐ์ง๋ง JavaScript
๊ฐ .innerText
๊ฐ ์๋ .value
๋ก ์ ๊ทผํ๊ธฐ ๋๋ฌธ์
๋ง์ฐฌ๊ฐ์ง๋ก v-model
directive ๋ฅผ ์ด์ฉํด
<textarea v-model=โsomeValueโ></textarea> ๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
<template>
<textarea v-model="someValue"></textarea>
</template>
<script>
export default {
data() {
return {
someValue: `Some Text`
}
},
}
</script>
6. Form select
v-model
directive ๋ฅผ ์ด์ฉํด
<select v-model=โsomeValueโ></select> ๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
<template>
<select v-model="someValue">
<option value="value01">Value 01</option>
<option value="value02">Value 02</option>
<option value="value03">Value 03</option>
</select>
</template>
<script>
export default {
data() {
return {
someValue: `value01`
}
},
}
</script>
๋ํ ์ฌ์ฉ์ ์
๋ ฅ๊ฐ์ด ์๋ ๊ณ ์ ๊ฐ์ด ์ง์ ๋๋ select box
๋๋ radio button
์ ๊ฒฝ์ฐ value
๋ฅผ HTML ์ ๊ฐ์ ๊ณ ์ ํ์ง
์๊ณ v-bind:value
directive ๋ฅผ ์ด์ฉํด ๋ ๋๋ง ํ ์ ์๋ค.
<option v-bind:value=โvalue01โ></option> ๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
<template>
<select v-model="someValue">
<option v-bind:value="value01">Value 01</option>
<option v-bind:value="value02">Value 02</option>
<option v-bind:value="value03">Value 03</option>
</select>
</template>
<script>
export default {
data() {
return {
someValue: `value01`,
value01: `value01`,
value02: `value02`,
value03: `value03`,
}
},
}
</script>
7. Form input type="checkbox"
JavaScript
๊ฐ .value
๊ฐ ์๋ .checked
๋ก ์ ๊ทผํ์ง๋ง
value
์ ๊ทผ์ผ๋ก ์ทจ๊ธํด v-model
directive ๋ฅผ ์ด์ฉํด
<input type=โcheckboxโ v-model=โsomeBooleanโ> ๋ก ๋ฐ์ธ๋ฉ ํ๋ค.
<template>
<input type="checkbox" v-model="someBoolean">
</template>
<script>
export default {
data() {
return {
someBoolean: true
}
},
}
</script>
8. Form input type="radio"
6. select box ์ ๋์ผํ๊ฒ v-model
๊ณผ v-bind:value
๋ฅผ ์ฌ์ฉํ๋ค.
์๋
radio button
์name
์ ๋์ผํ๊ฒ ํด์ค์ผํ์ง๋ง, ๋ณ๋๋กname
์ ์ค์ ํ์ง ์์๋v-model
๋ฐ์ธ๋ฉ์ ์ํด ํ๋์ ๊ฐ๋ง ์ ํ๋๋ค.
<template>
<select v-model="someValue">
<option value="value01">Value 01</option>
<option value="value02">Value 02</option>
<option value="value03">Value 03</option>
</select>
<div>
<label for="radio01"><input type="radio" v-bind:value="value01" id="radio01" v-model="someChecked">์์ธ</label>
<label for="radio02"><input type="radio" v-bind:value="value02" id="radio02" v-model="someChecked">๋ถ์ฐ</label>
<label for="radio03"><input type="radio" v-bind:value="value03" id="radio03" v-model="someChecked">์ ์ฃผ</label>
</div>
</template>
<script>
export default {
data() {
return {
someChecked: `value01`
}
},
}
</script>
<template>
<select v-model="someValue">
<option v-bind:value="value01">Value 01</option>
<option v-bind:value="value02">Value 02</option>
<option v-bind:value="value03">Value 03</option>
</select>
<div>
<label for="radio01"><input type="radio" v-bind:value="value01" id="radio01" v-model="someChecked">์์ธ</label>
<label for="radio02"><input type="radio" v-bind:value="value02" id="radio02" v-model="someChecked">๋ถ์ฐ</label>
<label for="radio03"><input type="radio" v-bind:value="value03" id="radio03" v-model="someChecked">์ ์ฃผ</label>
</div>
</template>
<script>
export default {
data() {
return {
someChecked: `value01`,
value01: `value01`,
value02: `value02`,
value03: `value03`,
}
},
}
</script>
6. Data Binding โv-bind:xโ for Attributes ๐ฉโ๐ป
HTML ์ attributes ๋ฅผ ๋ฐ์ธ๋ฉ ํ ๋ value
๋ฅผ ์ ์ธํ attributes ๋ v-bind:x
๋ฅผ ์ด์ฉํด ๋ฐ์ธ๋ฉ ํ๋ฉฐ, v-bind
๋ฅผ
์๋ตํด :x
๋ก ๋ฐ์ธ๋ฉ ํ ์ ์๋ค.
1. img > src
v-bind:scr
directive ๋ฅผ ์ด์ฉํ๋ค.
<template>
<img :src="myBlogFavicon">
</template>
<script>
export default {
data() {
return {
myBlogFavicon: `https://sbpark88.github.io/assets/images/favicon/greendreamtree.png`
}
},
}
</script>
2. button > disabled
v-bind:diabled
directive ๋ฅผ ์ด์ฉํ๋ค.
<template>
<input type="text" v-model="textValue">
<button type="button" :disabled="!textValue">Click</button>
</template>
<script>
export default {
data() {
return {
textValue: ``
}
},
}
</script>
3. class
.classList.add('someClass')
๋๋ .classList.remove('someClass')
๋ฅผ ํ ํ์ ์์ด,
๋ฐ๋์ ์ฌ์ฉํ class
๋ class
attribute ๋ฅผ ์ฌ์ฉํ๊ณ , Vue
๋ฅผ ์ด์ฉํด ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ ๋ณ๋์ฑ ์๋ class
๋
v-bind:class
directive ๋ฅผ ์ด์ฉํ๋ฉฐ, Object
ํ์
๋งคํ๊ณผ, Array
ํ์
๋งคํ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
1 ) v-bind:class by Object Mapping
<template>
<div class="fixed-class" :class="{ 'some-class-1': class1, 'some-class-2': class2 }"></div>
</template>
<script>
export default {
data() {
return {
class1: true,
class2: false
}
},
}
</script>
<style scoped>
.fixed-class { }
.some-class-1 { }
.some-class-2 { }
</style>
class-name: boolean
์์ผ๋ก ์์ฑํด ์ ์ฉํ ํด๋์ค๋ฅผ On/Off ํ ์ ์๋ค.
2 ) v-bind:class by Array Mapping
<template>
<div class="fixed-class" :class="[class1, class2]"></div>
</template>
<script>
export default {
data() {
return {
class1: 'some-class-1',
class2: 'some-class-2'
}
},
}
</script>
<style scoped>
.fixed-class { }
.some-class-1 { }
.some-class-2 { }
</style>
ํด๋์ค ์ด๋ฆ์
Array
๋ก ๋์ดํ ๋ฟ ์ ์ฉํ ํด๋์ค๋ฅผ On/Off ํ ์ ์๋ค.
๋์Array
๋ฅผ ์ด์ฉํ๋ฉด ์ ์ผ์ด์ค๋ฅผ ๊ธฐ์ค์ผ๋กclass1
๋ณ์ ํ๋์some-class-1 some-class-2
์ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
4. Inline Style
v-bind:style
directive ๋ฅผ ์ด์ฉํ๋ฉฐ, Object
ํ์
๋งคํ๊ณผ, Array
ํ์
๋งคํ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
๋จ, v-bind:class
์ ๋ฌ๋ฆฌ Object Mapping
์ด template
์ชฝ์ด ์๋ data()
์ชฝ์์ ์ด๋ค์ง๋ค. ์ฆ, boolean
์ด
์๋ CSS
์ Property: Value
์์ด ๋งคํ๋๋ค.
1 ) v-bind:style by Object Mapping
<template>
<div :style="styleObject"></div>
</template>
<script>
export default {
data() {
return {
styleObject: {
proeprty1: 'value1',
proeprty2: 'value2'
}
}
},
}
</script>
2 ) v-bind:style by Array Mapping
<template>
<div :style="[style1, style2]"></div>
</template>
<script>
export default {
data() {
return {
style1: 'property1: value1; property2: value2;',
style2: 'proeprty3: value3;'
}
},
}
</script>
7. v-for
select
์ option
, table
์ tr
๋ฐ์ดํฐ์ ๊ฐ์ ๋ฆฌ์คํธ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฃผ๋ก ์ฌ์ฉ๋๋ฉฐ,
v-for
directive ๋ฅผ ์ด์ฉํ๋ฉฐ, v-for="(item, index) in items"
์ ํํ๋ก ์ฌ์ฉํ๋ค.
<template>
<table>
<thead>
<tr>
<th>์ ํ๋ช
</th>
<th>๊ฐ๊ฒฉ</th>
<th>์นดํ
๊ณ ๋ฆฌ</th>
<th>๋ฐฐ์ก๋ฃ</th>
</tr>
</thead>
<tbody>
<tr v-for="(product, index) in productList" :key="index">
<td>{{ product.product_name }}</td>
<td>{{ product.price }}</td>
<td>{{ product.category }}</td>
<td>{{ product.delivery_price }}</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
name: "VueFor",
data() {
return {
productList: [
{"product_name": "์ฌ๊ณผ", "price": 4000, "category": "๊ณผ์ผ", "delivery_price": 3000},
{"product_name": "๋ฐฐ", "price": 6000, "category": "๊ณผ์ผ", "delivery_price": 3000},
{"product_name": "์ฐธ์น", "price": 30000, "category": "์์ ", "delivery_price": 10000},
{"product_name": "์์ฌ", "price": 40000, "category": "์ก๋ฅ", "delivery_price": 6000},
{"product_name": "์์ธ", "price": 12000, "category": "์ฃผ๋ฅ", "delivery_price": 0},
]
}
}
}
</script>
<style scoped>
table {
font-family: Arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #ddd;
text-align: left;
padding: 8px;
}
</style>
8. v-if, v-show
1. v-if
<template>
<div>
<h1 v-if="type === 'A'">A</h1>
<h1 v-else-if="type === 'B'">B</h1>
<h1 v-else>Others</h1>
</div>
</template>
<script>
export default {
name: "VueIf",
data() {
return {
type: 'B'
}
},
}
</script>
2. v-show
<template>
<div>
<h1 v-show="type === 'A'">A</h1>
<h1 v-show="type === 'B'">B</h1>
<h1 v-show="!(type === 'A' || type === 'B')">Others</h1>
</div>
</template>
<script>
export default {
name: "VueSHow",
data() {
return {
type: 'B'
}
}
}
</script>
v-if
๋ DOM ์ ์์ฑ/์ญ์ ํ๊ณ ,v-show
๋ DOM ์ ๋ชจ๋ ์์ฑ ํdisplay=none
์ฒ๋ฆฌ๋ง ํ๋ค.
๋ฐ๋ผ์ ์ต์ด DOM ์์ฑ์v-show
๊ฐ ๋ ๋ง์ ๋น์ฉ์ด ๋ค์ง๋ง, ์ดํ ํ๋ฉด์์ ์์ฑ/์ญ์ ๊ฐ ์ผ์ด๋ ๋๋ ๊ฐ์ถ๊ธฐ๋ง ํ๋ฏ๋ก ๋ ์ ์ ๋น์ฉ์ผ๋ก ์๋ํ๋ค. ๋ฐ๋ผ์ ํด๋น ๋ณ์์ ํ ๊ธ์ด ์์ฃผ ์ผ์ด๋๋ค๋ฉดv-show
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
9. v-on
HTML
์ onclick
, onchange
, onkeyup
๊ณผ ์ฌ์ฉ๋ฒ์ด ๋์ผํ๋ค.
์ฐจ์ด์ ์ Vue
์ ์ํด ๊ด๋ฆฌ๋ ์ ์๋๋ก v-on:x
directive ๋๋ @x
directive ๋ฅผ ์ฌ์ฉํด Vue
์ methods
์
์ฐ๊ฒฐํด์ฃผ๋ ๊ฒ๋ง ๋ค๋ฅด๋ค.
v-on:click="someMethodName"
๋๋ @click="someMethodName"
์ ํํ๋ก ์ฌ์ฉํ๋ค.
1. v-on:click
- Whitout Arguments
<template>
<button type="button" @click="increaseCounter">Add 1</button>
<p>The counter is : {{ counter }}</p>
</template>
<script>
export default {
name: "VueOnEvents",
data() {
return {
counter: 0
}
},
methods: {
increaseCounter() {
this.counter++
}
},
}
</script>
- With Arguments
<template>
<button type="button" @click="resetCounter(0)">Reset</button>
<p>The counter is : {{ counter }}</p>
</template>
<script>
export default {
name: "VueOnEvents",
data() {
return {
counter: 0,
}
},
methods: {
resetCounter(value) {
this.counter = value
}
},
}
</script>
- Pseudo Pipe
์ ํํ reduce
๋ฅผ ์ด์ฉํ Pipe
ํจ์์๋ ์กฐ๊ธ ๋ค๋ฅด์ง๋ง, data
์ ๋ณ์๋ฅผ ์์ฑํด ๋ฉ์๋๋ฅผ ์ฐ์ํด ํธ์ถํ๋ฏ๋ก์จ
์ ์ฌ Pipe
๊ตฌํ๋ ๊ฐ๋ฅํ๋ค.
<template>
<button type="button" @click="first(pipeInitialValue = 5), second(pipeInitialValue)">Multi Call</button>
</template>
<script>
export default {
name: "VueOnEvents",
data() {
return {
pipeInitialValue: 0
}
},
methods: {
first(value) {
alert(value)
this.pipeInitialValue += 10
},
second(value) {
alert(value)
}
},
}
</script>
5
15
2. v-on:change
<template>
<select v-model="selectedValue" @change="popSelected">
<option v-for="(city, index) in cities" :key="index" :value="city">{{ city }}</option>
</select>
</template>
<script>
export default {
name: "VueOnEvents",
data() {
return {
cities: ["์์ธ", "๋ถ์ฐ", "์ ์ฃผ"],
selectedValue: "",
}
},
methods: {
popSelected() {
alert(this.selectedValue)
},
},
}
</script>
<style scoped>
</style>
3. v-on:keyup
Vue
๋ keyup
์ด๋ฒคํธ์ ํน์ ํค ์กฐ๊ฑด๋ฌธ์ ์ง์ ์ ํ์ง ์๊ณ ๋ ์์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค. ์ด์์ฒด์ ์ ๋ฐ๋ผ key-code
๊ฐ
๋ค๋ฅธ ๊ฒ์ ๋ํด ๋ณ๋๋ก ์ฒ๋ฆฌํ ํ์๊ฐ ์๋ค.
- @keyup.enter : enter, return
- @keyup.tab
- @keyup.delete : delete, backspace
- @keyup.esc
- @keyup.space
- @keyup.up
- @keyup.down
- @keyup.left
- @keyup.right
๋ํ, ๋ค์๊ณผ ๊ฐ์ด ํค ์กฐํฉ์ ๋ํ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ ๊ฐ๋ฅํ๋ค.
- @keyup.alt.enter
- @click.ctrl
<template>
<h4>Keyup (Win: alt + enter / Mac: option + return)</h4>
<input type="text" @keyup.alt.enter="altEnterCalled">
<div style="display: grid">
<div class="btn-style" @click.meta="commandClickCalled">Command Click</div>
</div>
</template>
<script>
export default {
name: "VueOnEvents",
data() {
return { }
},
methods: {
altEnterCalled() {
alert(`(alt + enter) or (option + return) called!!`)
},
commandClickCalled() {
alert(`(win + click) or (command + click) called!!`)
},
},
}
</script>
<style scoped>
.btn-style {
justify-self: center;
width: 200px;
padding: 5px 10px;
margin: 10px;
background-color: burlywood;
border: blanchedalmond solid 3px;
border-radius: 10px;
}
</style>
10. Computed, Watch
1. Computed Properties
Swift Computed Properties ์ ๊ฐ๋ค.
๊ฐ์ ๊ณ์ฐํด ๋ณ์๋ก ์ฌ์ฉํ๋ฉฐ, ๊ฐ์ ์บ์ฑ๋๋ค.
- Getter/Setter
<template>
<h4>Perform every times</h4>
<div>{{ `${firstName} ${lastName}` }}</div>
<h4>Computed</h4>
<div>{{ fullName }}</div>
<hr>
<h4>firstName</h4>
<input type="text" v-model="firstName">
<h4>lastName</h4>
<input type="text" v-model="lastName">
<h4>fullName</h4>
<input type="text" v-model="fullName">
</template>
<script>
export default {
name: "VueComputedAndWatch",
data() {
return {
firstName: "Harry",
lastName: "Potter"
}
},
methods: {},
computed: {
fullName: {
get: function () {
return `${this.firstName} ${this.lastName}`
},
set: function (newValue) {
const names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[1]
}
}
}
}
</script>
- Read-Only
<template>
<h4>Perform every times</h4>
<div>{{ `${firstName} ${lastName}` }}</div>
<h4>Computed</h4>
<div>{{ fullName }}</div>
<hr>
<h4>firstName</h4>
<input type="text" v-model="firstName">
<h4>lastName</h4>
<input type="text" v-model="lastName">
<h4>fullName</h4>
<input type="text" v-model="fullName">
</template>
<script>
export default {
name: "VueComputedAndWatch",
data() {
return {
firstName: "Harry",
lastName: "Potter"
}
},
methods: {},
computed: {
fullName: function() {
return `${this.firstName} ${this.lastName}`
},
}
}
</script>
Read-Only ์ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์๋ ์๋ค.
computed: { fullName() { return `${this.firstName} ${this.lastName}` }, }
Computed Properties ์
Getter/Setter
์Arrow Functions
๋ ์ฌ์ฉํ ์ ์๋ค.
2. Watch(Property Observers)
Swift Property Observers ์์ didSet
๋ง ์กด์ฌํ๋ ๊ฒ๊ณผ ๊ฐ๋ค.
<template>
<h4>Perform every times</h4>
<div>{{ `${firstName} ${lastName}` }}</div>
<h4>Computed</h4>
<div>{{ fullName }}</div>
<hr>
<h4>firstName</h4>
<input type="text" v-model="firstName">
<h4>lastName</h4>
<input type="text" v-model="lastName">
<h4>fullName</h4>
<input type="text" v-model="fullName">
</template>
<script>
export default {
name: "VueComputed",
data() {
return {
firstName: "Harry",
lastName: "Potter"
}
},
methods: {},
computed: {
fullName: {
get: function () {
return `${this.firstName} ${this.lastName}`
},
set: function (newValue) {
const names = newValue.split(' ')
this.firstName = names[0]
this.lastName = names[1]
}
}
},
watch: {
firstName(newValue, oldValue) {
console.log(`firstName(old) : ${oldValue}`)
this.firstName = `${newValue.substring(0, 1).toUpperCase()}${newValue.substring(1)}`
},
lastName(newValue, oldValue) {
console.log(`lastName(old) : ${oldValue}`)
this.lastName = `${newValue.substring(0, 1).toUpperCase()}${newValue.substring(1)}`
},
}
}
</script>
Reference
- ๊ณ ์น์. Vue.js ํ๋ก์ ํธ ํฌ์ ์ผ์ฃผ์ผ ์ . ๋น์ ์ดํผ๋ธ๋ฆญ Chapter 4, 2021.
- ๊ณ ์น์. Vue.js ํ๋ก์ ํธ ํฌ์ ์ผ์ฃผ์ผ ์ . ๋น์ ์ดํผ๋ธ๋ฆญ Chapter 5, 2021.