用$ref调用 

直接代码:
子组件:
<template>
  <div>

  </div>
</template>

<script>
  export default {
    data(){
      return {
        num:'123'
      }
    },
    computed: {
    },
    components: {      
      'children': children
    },
    methods:{
      childMethod() {
        alert('childMethod do...')
      }
    },
    created(){
    }
  }
</script>


父组件

父组件: 在子组件中加上ref即可通过this.$refs.ref.method调用
<template>
  <div @click="parentMethod">
    <children ref="c1"></children>
  </div>
</template>

<script>
  import children from 'components/children/children.vue'
  export default {
    data(){
      return {
      }
    },
    computed: {
    },
    components: {      
      'children': children
    },
    methods:{
      parentMethod() {
        console.log(this.$refs.c1) //返回的是一个vue对象,所以可以直接调用其方法
        this.$refs.c1.childMethod(); 
      }
    },
    created(){
    }
  }
</script>
//子组件给父组件传值,必须有事件触发,参考网址:https://blog.csdn.net/qq_39985511/article/details/80062212
Logo

前往低代码交流专区

更多推荐