vue,父組件調用子組件方法

父組件調用子組件,利用$refs獲取子組件的方法

//子組件
export default {
    methods:{
        prompts(txt){
            alert(txt)
        }
    }
}
//父組件
<template>
    <myChild ref="child"></myChild>
    <button @click="btn('我是子組件')">確定</button>
</template>
export default{
    import myChild from '@/components/myChild'
    export default{
        components:{
            myChild
        },
        methods:{
            btn(txt){
                this.$refs.child.prompts(txt)//通過$refs找到子組件,並找到方法執行
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章