這篇文章主要介紹了vue 父組件中調用子組件函數的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑑價值 ,需要的朋友可以參考下
在父組件中調用子組件的方法:
1.給子組件定義一個ref屬性。eg:ref="childItem"
2.在子組件的methods中聲明一個函數。eg: useInPar:function (str) {console.log(str)}
2. 在父組件的中聲明一個函數,並通過this.$refs.childItem.userInPar
來使用子組件中聲明的函數。
父組件:
<template> <child-item ref='child' /> <button @click='useChildFun'></button> </template> <script> ``` methods() { useChildFun:function(){ this.$refs.child.usedInPar('調用子組件中的方法'); } } </script>
子組件:
``` methods () { usedInPar(str){ console.log(str); } }
沒有完整的代碼,只有一個用法
總結
以上所述是小編給大家介紹的vue 父組件中調用子組件函數的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!