VUE.JS
emit & props를 이용한 같은 레벨의 component 데이터 통신
JUMP개발자
2020. 4. 30. 15:27
이전에 살펴보았던 emit과 props를 사용하기 떄문에 큰 어려운 점 없이 component간의 데이터 통신이 가능하다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- same level 통신 content에서 header로 데이터 전송
: content에서 상위 컴포넌트(Vue)로 emit을 이용하여 전송하고,
다시 Vue에서 props를 이용하여 content로 전송. -->
<div id="app">
<!-- 6. propsdata를 이용하여 num 데이터 출력-->
<app-header v-bind:propsdata="num"></app-header>
<!--3. $emit('pass')를 이용해서 상위 컴포넌트의 deliverNum 메서드 호출-->
<app-content v-on:pass="deliverNum"></app-content>
</div>
<script>
// 5. props 사용.
var appHeader = {
template: '<div>{{propsdata}}</div>',
props: ['propsdata']
}
var appContent = {
// 1. 클릭 이벤트 발생
template: '<div>content<button v-on:click="passNum">pass</button></div>',
methods: {
// 2. 데이터 10 을 넘김
passNum: function() {
this.$emit('pass', 10);
}
}
}
new Vue({
el: '#app',
components: {
'app-header': appHeader,
'app-content': appContent
},
data: {
num: 0
},
methods: {
// 4. data num 변수에 파라미터를 담음.
deliverNum: function(value) {
this.num = value;
}
}
})
</script>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|