在子组件中调用父组件函数
直接使用this.$parent.event
<template><div><child></child></div></template><script>import child from '~/components/dam/child';export default {components: {child},methods: {fatherMethod() {console.log('测试');}}};</script>
<template><div><button @click="childMethod()">点击</button></div></template><script>export default {methods: {childMethod() {this.$parent.fatherMethod();}}};</script>
通过this.$emit向父组件触发一个事件
第三种是父组件把方法传入子组件中,在子组件里直接调用这个方法
<template><div><child :fatherMethod="fatherMethod"></child></div></template><script>import child from '~/components/dam/child';export default {components: {child},methods: {fatherMethod() {console.log('测试');}}};</script>
<template><div><button @click="childMethod()">点击</button></div></template><script>export default {props: {fatherMethod: {type: Function,default: null}},methods: {childMethod() {if (this.fatherMethod) {this.fatherMethod();}}}};</script>
在父组件中调用子组件函数
1.给子组件定义一个ref属性。ref=“childItem”
2.在子组件的methods中声明一个函数。
getData:function (str) {console.log(str)}
3. 在父组件的中声明一个函数,并通过this.$refs.childItem.getData来使用子组件中声明的函数。
