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


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


this.$parent.數據

this.$parent.方法

 

在子組件Header.vue里面

<template>


    <div>
    
        <h2>我是頭部組件</h2>

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

       
    </div>
</template>
------------------------------------------------------------------------
<script>
    
export default{


    data(){

        return{
            msg:'子組件的msg'
        }
    },
    methods:{
       
            run(){

                alert('我是子組件的run方法')
            },
            getParentData(){


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


                this.$parent.數據

                this.$parent.方法

                
                */
                alert(this.$parent.msg);//數據

                this.$parent.run(); //方法
            }
    }
    
}

</script>

 


免責聲明!

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



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