$parent

    $parent 用于 子传父

    值为对象,当前组件的父组件实例对象。

    1<template>
    2  <h2>父组件</h2>
    3  <p>{{ num }}</p>
    4  <hr/>
    5  <child-comp/>
    6
    7</template>
    8
    9<script lang="ts" name="ParentComp" setup>
    10import ChildComp from "@/components/ChildComp.vue";
    11import {ref} from "vue";
    12
    13let num = ref(10)
    14defineExpose({num})
    15</script>
    1<template>
    2  <h2>子组件</h2>
    3  <button @click="change($parent)">修改父组件数据</button>
    4  <hr/>
    5</template>
    6
    7<script lang="ts" name="ChildComp" setup>
    8function change(parent) {
    9  parent.num -= 1
    10}
    11</script>

    :::