vue父子组件——$refs

<!-- 子组件 -->
<template>
    <div class='container'>
        <div>子组件</div>
    </div>
</template>

<script>
    export default {
        name: 'Child',
        methods: {
            btn() {
                console.log('我是子组件一个普通方法')
            }
        }
    }
</script>
<!-- 父组件 -->
<template>
    <div class='container'>
        <div>父组件</div>
        <Child ref="child"></Child>
        <button @click="button">按钮</button>
    </div>
</template>

<script>
    import Child from '@/components/Child'
    export default {
        components: { Child },
        methods: {
            button() {
                this.$refs.child.btn()
            }
        }
    }
</script>

你可能感兴趣的:(前端,js)