vue2ref的理解?

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>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容