vue 父組件中調用子組件函數



2019/06/06

在父組件中調用子組件的方法:

 

 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);

   }

 }

 

 

沒有完整的代碼,只有一個用法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM