(1)第一种情况: count2根据count计算得到

    1. <template>
    2. <div style="margin: 30px">
    3. {{ count }}
    4. </div>
    5. <div style="margin: 30px">
    6. {{ count2 }}
    7. </div>
    8. </template>
    9. <script>
    10. import { computed, ref, watch } from "vue";
    11. export default {
    12. setup(props) {
    13. const count = ref(1);
    14. const count2 = computed(() => count.value + 10);
    15. return {
    16. count,
    17. count2,
    18. };
    19. },
    20. };
    21. </script>

    (2) 第二种情况: count2根据count得到, 修改count2的同时也修改count的值

    1. <template>
    2. <div style="margin: 30px">
    3. {{ count }}
    4. </div>
    5. <div style="margin: 30px">
    6. {{ count2 }}
    7. </div>
    8. </template>
    9. <script>
    10. import { computed, ref, watch } from "vue";
    11. export default {
    12. setup(props) {
    13. const count = ref(1);
    14. const count2 = computed({
    15. get: () => count.value + 1,
    16. set: (val) => {
    17. count.value = val - 1;
    18. },
    19. });
    20. setTimeout(()=> {
    21. count2.value = 10;
    22. },2000)
    23. return {
    24. count,
    25. count2,
    26. };
    27. },
    28. };
    29. </script>