vue 子元件主動獲取父元件的資料和方法
阿新 • • 發佈:2018-11-09
子元件主動獲取父元件的資料和方法:
this.$parent.資料
this.$parent.方法
在子元件Header.vue裡面
<template> <div> <h2>我是頭部元件</h2> <button @click="getParentData()">獲取子元件的資料和方法</button> </div> </template> ------------------------------------------------------------------------ <script> exportdefault{ data(){ return{ msg:'子元件的msg' } }, methods:{ run(){ alert('我是子元件的run方法') }, getParentData(){ /* 子元件主動獲取父元件的資料和方法: this.$parent.資料 this.$parent.方法*/ alert(this.$parent.msg);//資料 this.$parent.run(); //方法 } } } </script>