Skip to content

3 - 响应性丟失 toRefs解决,问题涉及深拷贝 #2193

Open
@zxxj

Description

@zxxj
<script setup lang="ts">
import { reactive, toRefs, toRef } from "vue"

function useCount() {
  const state = reactive({
    count: 0,
  })

  function update(value: number) {
    state.count = value
  }

  return {
    state: toRefs(state),
    update,
  }
}

// Ensure the destructured properties don't lose their reactivity
const { state: { count }, update } = useCount()
// 利用toRefs解决解构之后失去响应式问题,涉及深拷贝
</script>

<template>
  <div>
    <p>
      <span @click="update(count-1)">-</span>
      {{ count }}
      <span @click="update(count+1)">+</span>
    </p>
  </div>
</template>

Activity

changed the title [-]3 - 响应性丟失[/-] [+]3 - 响应性丟失 toRefs解决,问题涉及深拷贝[/+] on Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zxxj

        Issue actions

          3 - 响应性丟失 toRefs解决,问题涉及深拷贝 · Issue #2193 · webfansplz/vuejs-challenges