vue父組件主動獲取子組件的數據和方法


父組件主動獲取子組件的數據和方法:

1.調用子組件的時候定義一個ref

<v-header ref="header"></v-header>

2.在父組件里面通過

this.$refs.header.屬性

this.$refs.header.方法

<template>
    <!-- 所有的內容要被根節點包含起來 -->
    <div id="home">
    
        <v-header ref="header"></v-header>

        <hr>
         首頁組件   

         <button @click="getChildData()">獲取子組件的數據和方法</button>

    </div>

</template>
-------------------------------------------------------------------
  methods:{

            run(){

                alert('我是Home組件的run方法');
            },
            getChildData(){

                //父組件主動獲取子組件的數據和方法:
                // alert(this.$refs.header.msg);//數據

                this.$refs.header.run();//方法
            }
        }

 


免責聲明!

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



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