This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path13-parents-children-ref.html
94 lines (78 loc) · 1.82 KB
/
13-parents-children-ref.html
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>
<title></title>
<style>
#app {
margin: 1em;
font-size: 1.2em;
}
.component div {
margin-bottom: 1em;
}
</style>
</head>
<body>
<div id="app">
<my-component ref="comp1" :parent-msg="msg"></my-component>
<my-component2 ref="comp2"></my-component2>
<my-component3 ref="comp3"></my-component3>
<hr>
<div> {{ msg }}</div>
</div>
<script type="text/x-template" id="my-component">
<div class="component">
<div> ParentMsg: {{ parentMsg }} </div>
<div> ChildMsg: {{ msg }} </div>
</div>
</script>
<script>
Vue.component('my-component2', {
template: '<div>{{ msg }}</div>',
data() {
return {
msg: 'Hi COMP2'
}
}
});
Vue.component('my-component3', {
template: '<div>{{ msg }}</div>',
data() {
return {
msg: 'Hi COMP3'
}
}
});
// register
Vue.component('my-component', {
props: ["parentMsg"],
template: '#my-component',
mounted() {
console.log("$parent: ", this.$parent.msg);
window.setTimeout(() => {
this.$parent.msg = 'HELLO!';
}, 3000);
},
data: function() {
return {
msg: 'Msg of Child!'
}
}
});
// a root instance
new Vue({
el: '#app',
data: {
msg: 'Msg of Parent!'
},
mounted() {
console.log('$children1: ', this.$refs.comp1.msg);
console.log('$children2: ', this.$refs.comp2.msg);
console.log('$children3: ', this.$refs.comp3.msg);
}
});
</script>
</body>
</html>