+
80
-

uniapp子组件如何调用父组件方法?

uniapp子组件如何调用父组件方法?

网友回复

+
0
-

父组件

<template>
  <div>
  	// 第三步,监听子组件触发的fatherMethod事件,执行fatherMethods方法
    <child @fatherMethod="fatherMethods"></child>
  </div>
</template>
<script>
  import child from '~/components/child/child';
  export default {
    components: {
      child
    },
    methods: {
      // 第四步,执行方法并接收子组件的传值
      fatherMethods(e) {
        console.log('父组件方法',e);  // 父组件方法,0527
      }
    }
  };
</script>

子组件

<template>
  <div>
  	// 第一步
    <button @click="childMethod()">click</button>
  </div>
</template>
<script>
  export default {
    methods: {
      // 第二步
      childMethod() {
        this.$emit('fatherMethod', 0527);
      }
    }
  };
</script>

我知道答案,我要回答