vue子组件获取父组件方法
注:以下代码未使用esLint语法检查
父组件:
<template>
<div class="wrapper">
<cp_action @parentMethod="macSelect"></cp_action>
</div>
</template>
<script>
import ../components/action //引入子组件
export default{
components:{
cp_action
},
method:{
macSelect(){
//方法体
alert(123);
}
}
}
</script>
子组件:
<template>
<div class="bet-action">
<span class="mac-select" @click="childMethod">机选</span>
</div>
</template>
<script>
export default{
methods: {
childMethod() {
console.log('请求父组件方法');
this.$emit('parentMethod'); //使用$emit()引入父组件中的方法
}
},
}
</script>
本文介绍了一个简单的Vue.js应用中如何实现子组件调用父组件的方法。通过实例演示了使用$emit触发事件的方式来进行父子组件之间的通信。
603

被折叠的 条评论
为什么被折叠?



