小编Pai*_*Joy的帖子

为什么普通对象在 Vue3 中会自动变成响应式?

这篇文章中有一个例子。它说正常的物体不会有“反应性”。

我在这个codesandbox中做了测试,发现对普通对象(甚至是普通字符串)的改变可以自动改变视图。

<template>
  {{ personName }} <!-- will change to Amy, why? -->
  {{ person.name }} <!-- will change to Amy, why? -->
  {{ personRef.name }}
  {{ personReactive.name }}
  <button @click="changeName('Amy')">changeName</button>
</template>

<script setup>
import { ref, reactive } from "vue";

let personName = "John";
const person = { name: "John" };
const personRef = ref({ name: "John" });
const personReactive = reactive({ name: "John" });
const changeName = (name) => {
  personName = name;
  person.name = name; …
Run Code Online (Sandbox Code Playgroud)

vue.js vue-reactivity vuejs3 vue-composition-api

4
推荐指数
1
解决办法
251
查看次数