1.ref可以获取dom元素
2.ref可以子组件向父组件传递数据和方法
father组件:
<div>
<div ref="changefather">你好,我是你爸爸</div>
<son ref="changeson"/>
<button @click="changefather">打印father的信息</button>
<button @click="changeson">打印son的信息</button>
</div>
</template>
<script >
import son from '../components/son.vue'
export default {
components:{son},
methods:{
changeson(){
console.log(this.$refs.changeson.name) //滑翔
console.log(this.$refs.changeson.introduce(1,2)) //3
},
changefather(){
console.log(this.$refs.changefather) //<div>你好,我是你爸爸</div>
}
}
}
</script>
<style>
</style>
son组件:
<template>
<div>
<div>你好,我是你儿子</div>
<div>{{ name }}</div>
</div>
</template>
<script >
export default{
data(){
return{
name:'滑翔'
}
},
methods:{
introduce(a,b){
return a+b
}
}
}
</script>
<style>
</style>