File tree Expand file tree Collapse file tree 3 files changed +19
-3
lines changed
src/components/communication Expand file tree Collapse file tree 3 files changed +19
-3
lines changed Original file line number Diff line number Diff line change 14
14
},
15
15
},
16
16
mounted () {
17
- this .$bus .$on (' event-from-child2' , msg => {
17
+ // 总线方式通信
18
+ // this.$bus.$on('event-from-child2', msg => {
19
+ // console.log('Child1:', msg);
20
+ // });
21
+
22
+ // 利用共同祖辈方式通信
23
+ // 兄弟组件可以使用$parent, 没有直接关系使用$root
24
+ this .$parent .$on (' event-from-child2' , msg => {
18
25
console .log (' Child1:' , msg);
19
26
});
20
27
},
Original file line number Diff line number Diff line change 10
10
methods: {
11
11
sendToChild1 () {
12
12
// 利用事件总线发送事件
13
- this .$bus .$emit (' event-from-child2' , ' some msg from child2' )
13
+ // this.$bus.$emit('event-from-child2', 'some msg from child2')
14
+ // 利用共同祖辈元素发送事件
15
+ this .$parent .$emit (' event-from-child2' , ' some msg from child2' )
14
16
}
15
17
},
16
18
}
Original file line number Diff line number Diff line change 4
4
<!-- props, 自定义事件 -->
5
5
<Child1 msg =" some msg from parent" @some-event =" onSomeEvent" ></Child1 >
6
6
<!-- 事件总线 -->
7
- <Child2 ></Child2 >
7
+ <Child2 ref = " child2 " ></Child2 >
8
8
</div >
9
9
</template >
10
10
21
21
console .log (' Communition:' , msg);
22
22
}
23
23
},
24
+ mounted () {
25
+ // $children获取子组件数组,不包括普通元素,不保证模板中顺序
26
+ console .log (this .$children );
27
+ // $refs用于引用命名的元素或组件,可包含普通元素
28
+ console .log (this .$refs .child2 );
29
+
30
+ },
24
31
}
25
32
</script >
26
33
You can’t perform that action at this time.
0 commit comments