Skip to content

6 - 浅层 ref #2064

Open
Open
@ehxie

Description

@ehxie
<script setup lang="ts">
import { shallowRef, watch, triggerRef } from "vue"

const state = shallowRef({ count: 1 })

// Does NOT trigger
watch(state, () => {
  console.log("State.count Updated")
}, { deep: true })

/**
 * Modify the code so that we can make the watch callback trigger.
*/
state.value.count = 2
// 当修改了shallowRef包裹的复杂数据类型的时候 使用triggerRef函数手动通知vue响应
triggerRef(state)

</script>

<template>
  <div>
    <p>
      {{ state.count }}
    </p>
  </div>
</template>

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions