方案一:通过ref直接调用子组件的方法
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
//父组件中
"handleClick">点击调用子组件方法 "child"/>
"handleClick">点击调用子组件方法
"handleClick"
>点击调用子组件方法
"child"/>
"child"
/>
import Child from './child';
import Child from
'./child'
;
export default {
export
default
{
methods: {
handleClick() {
this.$refs.child.sing();
this
.$refs.child.sing();
},
}
//子组件中
我是子组件
sing() {
console.log('我是子组件的方法');
console.log(
'我是子组件的方法'
);
};
方案二:通过组件的$emit、$on方法
37
this.$refs.child.$emit("childmethod") //子组件$on中的名字
.$refs.child.$emit(
"childmethod"
)
//子组件$on中的名字
mounted() {
this.$nextTick(function() {
.$nextTick(
function
() {
this.$on('childmethods', function() {
.$on(
'childmethods'
,
console.log('我是子组件方法');
'我是子组件方法'
});
京公网安备 11010502049817号