|
@@ -0,0 +1,41 @@
|
|
|
+<template>
|
|
|
+ <div>
|
|
|
+ <h1>WatchEffect</h1>
|
|
|
+ <p>姓:
|
|
|
+ <input type="text" v-model="user.firstName">
|
|
|
+ </p>
|
|
|
+ <p>
|
|
|
+ 名:
|
|
|
+ <input type="text" v-model="user.lastName"/>
|
|
|
+ </p>
|
|
|
+ <h2>全名:
|
|
|
+ <input type="text" v-model="fullName">
|
|
|
+ </h2>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script lang="ts" setup>
|
|
|
+import {ref,reactive, watch, watchEffect} from "vue"
|
|
|
+interface Person {
|
|
|
+ firstName:string,
|
|
|
+ lastName:string
|
|
|
+}
|
|
|
+let user:Person = reactive({
|
|
|
+ firstName:'喜',
|
|
|
+ lastName:'羊羊'
|
|
|
+})
|
|
|
+const fullName = ref<string>("")
|
|
|
+// watch(user,({firstName,lastName}) => {
|
|
|
+// console.log(firstName,lastName)
|
|
|
+// fullName.value = `${firstName}${lastName}`
|
|
|
+// })
|
|
|
+// 自动的监听数据 发生改变 依赖也会随之变化
|
|
|
+watchEffect(()=>{
|
|
|
+ console.log(user,'user')
|
|
|
+ console.log(fullName.value,'fullName')
|
|
|
+ fullName.value = user.firstName + user.lastName
|
|
|
+})
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+</style>
|