Father.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <template>
  2. <div>
  3. <h1 class="h-26 text-6xl">$refs-$parent</h1>
  4. <p>我有{{ num }}辆车</p>
  5. <button @click="changeBook">修改书本数量</button>
  6. <hr class="h-15" />
  7. <button @click="changeFlower">修改花朵数量</button>
  8. <hr class="h-15" />
  9. <button @click="changeMoney($refs)">同时修改</button>
  10. <h3 class="h-16 text-4xl"></h3>
  11. <h3 class="h-16 text-4xl"></h3>
  12. <hr class="h-15" />
  13. <hr class="h-15" />
  14. <Child1 ref="a1"></Child1>
  15. <hr class="h-15" />
  16. <hr class="h-15" />
  17. <Child2 ref="a2"></Child2>
  18. </div>
  19. </template>
  20. <script lang="ts" setup>
  21. import { ref } from "vue";
  22. import Child1 from "./Child1.vue";
  23. import Child2 from "./Child2.vue";
  24. let a1 = ref();
  25. let a2 = ref();
  26. let num = ref(10);
  27. // console.log(a1, "a1");
  28. function changeBook() {
  29. a1.value.book += 1;
  30. }
  31. function changeFlower() {
  32. a2.value.flower += 2;
  33. }
  34. function changeMoney(val: { [propsName: string]: any }) {
  35. console.log(val);
  36. for (let key in val) {
  37. val[key].money -= 1000;
  38. }
  39. }
  40. defineExpose({ num });
  41. </script>
  42. <style lang="scss" scoped>
  43. </style>