Compare commits

..

2 Commits

Author SHA1 Message Date
云上贵猪
cf9a4c7f0d 使用 toRefs 让数据变为响应式的 2025-05-02 21:02:47 +08:00
云上贵猪
24be73a538 defineProps,实现简单的父传子 2025-05-02 20:44:48 +08:00
3 changed files with 66 additions and 52 deletions

65
.idea/workspace.xml generated
View File

@ -4,8 +4,9 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="82d74fc6-f54a-4255-8e7b-990e62fbaaf1" name="更改" comment="使用 Object.assign() 合并对象修改属性并更新页面区别于ref、reactive不可更新数据问题"> <list default="true" id="82d74fc6-f54a-4255-8e7b-990e62fbaaf1" name="更改" comment="defineProps实现简单的父传子">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/App.vue" beforeDir="false" afterPath="$PROJECT_DIR$/src/App.vue" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/components/Person.vue" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/Person.vue" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/components/Person.vue" beforeDir="false" afterPath="$PROJECT_DIR$/src/components/Person.vue" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -27,21 +28,21 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.git.unshallow": "true", &quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;,
"git-widget-placeholder": "dev", &quot;git-widget-placeholder&quot;: &quot;dev&quot;,
"node.js.detected.package.eslint": "true", &quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
"node.js.detected.package.tslint": "true", &quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
"node.js.selected.package.eslint": "(autodetect)", &quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
"node.js.selected.package.tslint": "(autodetect)", &quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
"nodejs_package_manager_path": "npm", &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
"settings.editor.selected.configurable": "preferences.keymap", &quot;settings.editor.selected.configurable&quot;: &quot;preferences.keymap&quot;,
"ts.external.directory.path": "/Volumes/Date/01_Demo/07-Vue3/node_modules/typescript/lib", &quot;ts.external.directory.path&quot;: &quot;/Volumes/Date/01_Demo/07-Vue3/node_modules/typescript/lib&quot;,
"vue.rearranger.settings.migration": "true" &quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
} }
}]]></component> }</component>
<component name="SharedIndexes"> <component name="SharedIndexes">
<attachedChunks> <attachedChunks>
<set> <set>
@ -58,6 +59,7 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1745919613888</updated> <updated>1745919613888</updated>
<workItem from="1745919615396" duration="326000" /> <workItem from="1745919615396" duration="326000" />
<workItem from="1746189663982" duration="1161000" />
</task> </task>
<task id="LOCAL-00001" summary="Merge remote-tracking branch 'vue3-学习仓库/dev' into dev&#10;&#10;# Conflicts:&#10;#&#9;.idea/workspace.xml&#10;#&#9;README.md&#10;#&#9;src/components/Person.vue"> <task id="LOCAL-00001" summary="Merge remote-tracking branch 'vue3-学习仓库/dev' into dev&#10;&#10;# Conflicts:&#10;#&#9;.idea/workspace.xml&#10;#&#9;README.md&#10;#&#9;src/components/Person.vue">
<option name="closed" value="true" /> <option name="closed" value="true" />
@ -107,12 +109,39 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1745934177134</updated> <updated>1745934177134</updated>
</task> </task>
<option name="localTasksCounter" value="7" /> <task id="LOCAL-00007" summary="ref、修改对象传入更新数据问题">
<option name="closed" value="true" />
<created>1745934687739</created>
<option name="number" value="00007" />
<option name="presentableId" value="LOCAL-00007" />
<option name="project" value="LOCAL" />
<updated>1745934687740</updated>
</task>
<task id="LOCAL-00008" summary="defineProps实现简单的父传子">
<option name="closed" value="true" />
<created>1746189895108</created>
<option name="number" value="00008" />
<option name="presentableId" value="LOCAL-00008" />
<option name="project" value="LOCAL" />
<updated>1746189895108</updated>
</task>
<option name="localTasksCounter" value="9" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" /> <option name="version" value="3" />
</component> </component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="Vue2 &amp; Vue3 中data 与setup 数据的调用情况" /> <MESSAGE value="Vue2 &amp; Vue3 中data 与setup 数据的调用情况" />
<MESSAGE value="Merge remote-tracking branch 'vue3-学习仓库/dev' into dev&#10;&#10;# Conflicts:&#10;#&#9;.idea/workspace.xml&#10;#&#9;README.md&#10;#&#9;src/components/Person.vue" /> <MESSAGE value="Merge remote-tracking branch 'vue3-学习仓库/dev' into dev&#10;&#10;# Conflicts:&#10;#&#9;.idea/workspace.xml&#10;#&#9;README.md&#10;#&#9;src/components/Person.vue" />
@ -121,6 +150,8 @@
<MESSAGE value="ref 变量 和 reaction 对象的使用" /> <MESSAGE value="ref 变量 和 reaction 对象的使用" />
<MESSAGE value="reaction 对象的使用 v-for" /> <MESSAGE value="reaction 对象的使用 v-for" />
<MESSAGE value="使用 Object.assign() 合并对象修改属性并更新页面区别于ref、reactive不可更新数据问题" /> <MESSAGE value="使用 Object.assign() 合并对象修改属性并更新页面区别于ref、reactive不可更新数据问题" />
<option name="LAST_COMMIT_MESSAGE" value="使用 Object.assign() 合并对象修改属性并更新页面区别于ref、reactive不可更新数据问题" /> <MESSAGE value="ref、修改对象传入更新数据问题" />
<MESSAGE value="defineProps实现简单的父传子" />
<option name="LAST_COMMIT_MESSAGE" value="defineProps实现简单的父传子" />
</component> </component>
</project> </project>

View File

@ -3,12 +3,8 @@ import Person from "@/components/Person.vue";
</script> </script>
<template> <template>
<header>
</header>
<main> <main>
<Person /> <Person/>
</main> </main>
</template> </template>

View File

@ -1,47 +1,34 @@
<script setup lang="ts"> <script setup lang="ts">
import {reactive, ref} from "vue"; import {reactive, toRefs} from "vue";
//let car = reactive( let person = reactive({
// {brand: '',price:100} name:'张三',
//) age:18,
let car = ref( })
{brand: '宝马',price:100}
)
function changeBrand(){ let {name,age} = toRefs(person)
car.value.brand = '三菱'
function changeName(){
//person.name = person.name + '~'
person.name += '~'
} }
function changePrice(){
car.value.price += 10
}
function changeCar(){
//car = {brand: '',price:5} //
//car = reactive({brand: '',price:5}) //
//Object.assign(car,{brand: '',price:5}) //
car.value = reactive({brand: '五菱宏观',price:5}) // ref function changeAge(){
person.age +=1
//
//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> </script>
<template> <template>
<main> <main>
<h1>汽车{{car.brand}}===价格{{car.price}}w</h1> <h1>姓名{{name}}</h1>
<button @click="changeBrand">修改名字</button> <h1>年龄{{age}}</h1>
<button @click="changePrice">价格+10</button> <button @click="changeName">修改名字</button>
<button @click="changeCar">整体修改</button> <button @click="changeAge">年龄+1</button>
</main> </main>
</template> </template>
<style scoped> <style scoped>
button { button {
margin: 0 1.5rem; margin: 0 1rem;
} }
</style> </style>