vue父组件如何调用子组件的属性或方法

时间:2020-05-01

子组件代码(Detail.vue)

<template>
    <p>
        子组件
    </p>
</template>

<script>
    export default {
        name: "detail",
        data() {
            return {
                name: '子组件'
            };
        },
        methods: {
            test() {
                console.log('子组件 test 方法');
            }
        }
    }
</script>

<style scoped>

</style>

父组件代码(Home.vue)

<template>
    <div>
        <Detail ref="Detail"></Detail>
        <button @click="test">调用子组件方法</button>
    </div>
</template>

<script>
    import Detail from "./Detail";

    export default {
        name: "home",
        components: {Detail},
        methods: {
            test() {
                this.$refs.Detail.test();
                console.log(this.$refs.Detail.name);
            }
        }
    }
</script>

<style scoped>

</style>