<Do it! Vue.js ์ ๋ฌธ>์ ๊ธฐ๋ณธ์ผ๋ก ๋ฐฐ์ด ๋ด์ฉ์ ๋๋ค.
๋ชฉ์ฐจ
- 1. ์·ํ์ ์ปดํฌ๋ํธ ๊ฐ ํต์
- 2. ๊ฐ์ ๋ ๋ฒจ์ ์ปดํฌ๋ํธ ๊ฐ ํต์
- 3. ๊ด๊ณ ์๋ ์ปดํฌ๋ํธ ๊ฐ ํต์
<Vue Componets ํต์ >
ํ ํ๋ฉด์ ์๋๋ผ๋ ๊ฐ ์ปดํฌ๋ํธ์ ์ ํจ๋ฒ์๋ ๋ ๋ฆฝ์ ์ด๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ์ปดํฌ๋ํธ์ ๊ฐ์ ์ง์ ์ ์ผ๋ก ์ฐธ์กฐํ ์ ์๋ค.
1. ์·ํ์ ์ปดํฌ๋ํธ ๊ฐ ํต์
์ง์ญ ๋๋ ์ ์ญ ์ปดํฌ๋ํธ๋ฅผ ๋ฑ๋กํ๋ฉด ๋ฑ๋ก๋ ์ปดํฌ๋ํธ๋ ์์ฐ์ค๋ฝ๊ฒ ์์ ์ปดํฌ๋ํธ๊ฐ ๋๊ณ , ์์ ์ปดํฌ๋ํธ๋ฅผ ๋ฑ๋กํ ์ธ์คํด์ค๋ ์์ ์ปดํฌ๋ํธ๊ฐ ๋๋ค.
1) ์์์์ ํ์ ์ปดํฌ๋ํธ๋ก ๋ฐ์ดํฐ ์ ๋ฌ(Pass Props)
ํ์ ์ปดํฌ๋ํธ์ ์์ฑ์ props์์ฑ์ ์ ์ํ๊ณ ,
Vue.component('child-component', {
props: ['props์์ฑ์ด๋ฆ']
});
์์ ์ปดํฌ๋ํธ์ HTML์ฝ๋์ ๋ฑ๋ก๋ child-component์ปดํฌ๋ํธ ํ๊ทธ์ v-bind์์ฑ์ ์ถ๊ฐํ๋ค.
<child-component v-bind:props ์์ฑ์ด๋ฆ = "์์ ์ปดํฌ๋ํธ์ data ์์ฑ"></child-component>
ex) ์๋ ์์ ์ฒ๋ผ ์ธ์คํด์ค(root ์ปดํฌ๋ํธ)์ ์๋ก์ด ์ปดํฌ๋ํธ๋ฅผ ๋ฑ๋กํ๋ฉด ๊ธฐ์กด์ ์๋ ์ปดํฌ๋ํธ๋ ์์(๋ถ๋ชจ)์ปดํฌ๋ํธ๊ฐ ๋๊ณ , ์๋ก ๋ฑ๋ก๋ ์ปดํฌ๋ํธ๋ ํ์(์์)์ปดํฌ๋ํธ๊ฐ ๋๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue Props Sample</title>
</head>
<body>
<div id="app">
<!-- ํ : ์ค๋ฅธ์ชฝ์์ ์ผ์ชฝ์ผ๋ก ์์ฑ์ ์ฝ์ผ๋ฉด ๋ ์์ํฉ๋๋ค. -->
<child-component v-bind:propsdata="message"></child-component>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.2/dist/vue.js"></script>
<script>
Vue.component('child-component', {
props: ['propsdata'],
template: '<p>{{ propsdata }}</p>',
});
new Vue({
el: '#app',
data: {
message: 'Hello Vue! passed from Parent Component'
}
});
</script>
</body>
</html>
2) ํ์์์ ์์ ์ปดํฌ๋ํธ๋ก ์ด๋ฒคํธ ์ ๋ฌ(Emit Events)
ํ์ ์ปดํฌ๋ํธ์ ํน์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด
this.$emit('์ด๋ฒคํธ๋ช
'); //์ด๋ฒคํธ ๋ฐ์
์์ ์ปดํฌ๋ํธ์์ ํด๋น ์ด๋ฒคํธ๋ฅผ ์์ ํ์ฌ ์์ ์ปดํฌ๋ํธ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค.
<child-component v-on:์ด๋ฒคํธ๋ช
="์์ ์ปดํฌ๋ํธ์ ๋ฉ์๋๋ช
"></child-component> //์ด๋ฒคํธ ์์
ex)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue Event Emit Sample</title>
</head>
<body>
<div id="app">
<child-component v-on:show-log="printText"></child-component>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.2/dist/vue.js"></script>
<script>
Vue.component('child-component', {
template: '<button v-on:click="showLog">show</button>',
methods: {
showLog: function() {
this.$emit('show-log');
}
}
});
new Vue({
el: '#app',
data: {
message: 'Hello Vue! passed from Parent Component'
},
methods: {
printText: function() {
console.log("received an event");
}
}
});
</script>
</body>
</html>
2. ๊ฐ์ ๋ ๋ฒจ์ ์ปดํฌ๋ํธ ๊ฐ ํต์
- ๋ทฐ๋ ์์์์ ํ์๋ก๋ง ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํด์ผ ํ๋ ๊ธฐ๋ณธ์ ์ธ ํต์ ๊ท์น์ ๋ฐ๋ฅธ๋ค.
- ๋ฐ๋ก ์ ์ปดํฌ๋ํธ์ ๊ฐ์ ์ ๋ฌํ๋ ค๋ฉด ํ์์์ ๊ณตํต ์์ ์ปดํฌ๋ํธ๋ก ์ด๋ฒคํธ๋ฅผ ์ ๋ฌํ ํ ๊ณตํต ์์ ์ปดํฌ๋ํธ์์ 2๊ฐ์ ํ์ ์ปดํฌ๋ํธ์ props์ ๋ด๋ ค์ผํ๋ค.
3. ๊ด๊ณ ์๋ ์ปดํฌ๋ํธ ๊ฐ ํต์ (์ด๋ฒคํธ ๋ฒ์ค)
1) ์ด๋ฒคํธ ๋ฒ์ค
- ์ค๊ฐ ์ปดํฌ๋ํธ๋ค์ ๊ฑฐ์น์ง ์๊ณ ํ์ ์ปดํฌ๋ํธ B์์ ์์ ์ปดํฌ๋ํธ A๋ก ๋ฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์๋ ๋ฐ์ดํฐ ์ ๋ฌ ๋ฐฉ์
- ์ด๋ฒคํธ ๋ฒ์ค๋ฅผ ํ์ฉํ๋ฉด props์์ฑ์ ์ด์ฉํ์ง ์๊ณ ๋ ์ํ๋ ์ปดํฌ๋ํธ ๊ฐ์ ์ง์ ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์์ด ํธ๋ฆฌํ์ง๋ง ์ปดํฌ๋ํธ๊ฐ ๋ง์์ง๋ฉด ์ด๋์ ์ด๋๋ก ๋ณด๋๋์ง ๊ด๋ฆฌ๊ฐ ๋์ง ์๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค. ์ด๋ฅผ Vuex๋ผ๋ ์ํ ๊ด๋ฆฌ ๋๊ตฌ๊ฐ ํ์ํ๋ค.
2) ์ด๋ฒคํธ ๋ฒ์ค ํ์
์ด๋ฒคํธ ๋ฒ์ค๋ฅผ ์ํ ์๋ก์ด ์ธ์คํด์ค๋ฅผ 1๊ฐ ์์ฑํ๊ณ ,
var eventBus = new Vue();
์ ์ธ์คํด์ค๋ฅผ ์ด์ฉํ์ฌ ์ด๋ฒคํธ๋ฅผ ๋ณด๋ด๊ณ ๋ฐ๋๋ค.
//์ด๋ฒคํธ๋ฅผ ๋ณด๋ด๋ ์ปดํฌ๋ํธ
methods: {
๋ฉ์๋๋ช
: function() {
eventBus.$emit('์ด๋ฒคํธ๋ช
', ๋ฐ์ดํฐ);
}
}
//์ด๋ฒคํธ๋ฅผ ๋ฐ๋ ์ปดํฌ๋ํธ
methods: {
created: function() {
eventBus.$on('์ด๋ฒคํธ๋ช
', funtion(๋ฐ์ดํฐ){
...
});
}
}
ex)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vue Event Bus Sample</title>
</head>
<body>
<div id="app">
<child-component></child-component>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.2/dist/vue.js"></script>
<script>
var eventBus = new Vue();
Vue.component('child-component', {
template: '<div>ํ์ ์ปดํฌ๋ํธ ์์ญ์
๋๋ค.<button v-on:click="showLog">show</button></div>',
methods: {
showLog: function() {
eventBus.$emit('triggerEventBus', 100);
}
}
});
var app = new Vue({
el: '#app',
created: function() {
eventBus.$on('triggerEventBus', function(value){
console.log("์ด๋ฒคํธ๋ฅผ ์ ๋ฌ ๋ฐ์. ์ ๋ฌ ๋ฐ์ ๊ฐ : ", value);
});
}
});
</script>
</body>
</html>
show๋ฒํผ์ ํด๋ฆญํ๋ฉด ์๋์ ๊ฐ์ consoleํ๋ฉด์ด ๋์จ๋ค.
'๐ปWEB FrontEnd > ํ๋ ์์ํฌ Vue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ทฐ HTTP ํต์ (0) | 2020.07.02 |
---|---|
Vue Router (0) | 2020.07.02 |
Vue Componets (0) | 2020.07.02 |
Vue Instance (0) | 2020.07.01 |
Vue ์์ํ๊ธฐ (+๊ฐ๋ฐ ํ๊ฒฝ ์ค์ ) (0) | 2020.06.30 |