使用$refs需要注意以下2點:
1.html方法使用子組件時,需使用ref = “xxx” 聲明.
2.在父組件中使用,this.refs.xxx.msg 獲取數據
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body><div id="app"><p>{{ message }}</p><my-component ref="comA"></my-component><button @click="handleRef">ref獲取子組件數據</button>
</div>
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<script>Vue.component('my-component',{template:'<button @click="sendMessage">派發事件</button>',data:function(){return {msg:'來自子組件'}}})const app = new Vue({el:'#app',data:{message: ''},methods:{handleRef:function(){this.message = this.$refs.comA.msg}}})
</script>
</body>
</html>
參考《Vue.js實戰》P82