子调父
1. 直接在子组件调用这个fatherMethod方法,这个方法父组件不可以v-if
this.$parent.fatherMethod();
2. 在父组件内将方法绑定在子组件身上,然后利用$emit调用fatherMethod方法
父组件
<template>
<div>
<child @fatherMethod="fatherMethod"></child>
</div>
</template>
子组件
export default {
methods: {
childMethod() {
this.$emit('fatherMethod');
}
}
};
父调子
1. 在父组件内为子组件绑定ref属性,由此可以直接调取子组件身上的方法
父组件
<template>
<div>
<button @click="handleClick">点击调用子组件方法</button>
<Child ref="child"/>
</div>
</template>
methods: {
handleClick() {
this.$refs.child.sing();
},
},