使用 Object.assign() 合并对象(修改属性并更新页面),区别于ref、reactive不可更新数据问题

This commit is contained in:
云上贵猪 2025-04-29 21:42:56 +08:00
parent 93379692b2
commit 2303e5b333
2 changed files with 32 additions and 29 deletions

15
.idea/workspace.xml generated
View File

@ -4,7 +4,7 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="82d74fc6-f54a-4255-8e7b-990e62fbaaf1" name="更改" comment="ref 变量 和 reaction 对象的使用">
<list default="true" id="82d74fc6-f54a-4255-8e7b-990e62fbaaf1" name="更改" comment="reaction 对象的使用 v-for">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/Person.vue" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/Person.vue" afterDir="false" />
</list>
@ -91,7 +91,15 @@
<option name="project" value="LOCAL" />
<updated>1745931199936</updated>
</task>
<option name="localTasksCounter" value="5" />
<task id="LOCAL-00005" summary="reaction 对象的使用 v-for">
<option name="closed" value="true" />
<created>1745932029058</created>
<option name="number" value="00005" />
<option name="presentableId" value="LOCAL-00005" />
<option name="project" value="LOCAL" />
<updated>1745932029058</updated>
</task>
<option name="localTasksCounter" value="6" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -103,6 +111,7 @@
<MESSAGE value="setup 的新写法" />
<MESSAGE value="ref 的简单实用" />
<MESSAGE value="ref 变量 和 reaction 对象的使用" />
<option name="LAST_COMMIT_MESSAGE" value="ref 变量 和 reaction 对象的使用" />
<MESSAGE value="reaction 对象的使用 v-for" />
<option name="LAST_COMMIT_MESSAGE" value="reaction 对象的使用 v-for" />
</component>
</project>

View File

@ -1,43 +1,37 @@
<script setup lang="ts">
import {reactive, ref} from "vue";
//
let name = ref('张三')
//
let car = reactive(
{brand: '小米 su 7', prive: 30}
{brand: '宝马',price:100}
)
//
let geame = reactive(
[
{id: '1', name: '王者荣耀'},
{id: '2', name: '黑神话·悟空'},
{id: '3', name: '地平线5'},
]
)
function geameFast(){
geame[0].name = '打wa'
function changeBrand(){
car.brand = '三菱'
}
function changeName() {
name.value = '李四'
function changePrice(){
car.price += 10
}
function changeCar(){
//car = {brand: '',price:5} //
//car = reactive({brand: '',price:5}) //
Object.assign(car,{brand: '五菱宏观',price:5}) //
function changePrive() {
//
car.prive += 10
//
//const target = { a: 1 };
//const source1 = { b: 2 };
//const source2 = { c: 3 };
//
//const result = Object.assign(target, source1, source2);
//console.log(result); // { a: 1, b: 2, c: 3 }
}
</script>
<template>
<main>
<h1>游戏列表</h1>
<ul>
<li v-for="g in geame" :key="g.id">{{g.name}}</li>
</ul>
<button @click="geameFast">修改第一个游戏明细</button>
<h1>汽车{{car.brand}}===价格{{car.price}}</h1>
<button @click="changeBrand">修改名字</button>
<button @click="changePrice">价格+10</button>
<button @click="changeCar">整体修改</button>
</main>
</template>