Vue3中组合API

Vue3中组合API中的侦听属性:watch

        1.侦听一个数据

                示例:

<script setup>
import{ref,watch} from 'vue'
const count = ref(0)
const setCount=()=>{
  count.value++
}
//调用侦听工具(函数)
watch(count, ()=>{
  console.log("侦听count,数据发生变化")
})
</script>
<template>
  <button @click="setCount">{{ count }}</button>
</template>

        2.侦听多个数据

                示例:

<script setup>
import{ref,watch} from 'vue'
const count = ref(0) 
const username = ref('小明')
const setCount=()=>{
  count.value++
}
const setUsername=()=>{
  username.value='小刘'
}
watch([count,username],([newCount,newUsername],[oldCount,oldUsername])=>{
  console.log("count或username的值发生变化了",[newCount,newUsername],[oldCount,oldUsername]) 
})
</script>
<template>
  <button @click="setCount">{{ count }}</button>
  <button @click="setUsername">{{ username }}</button>
</template>
<style scoped>
</style>

        3.深度侦听   默认情况属于浅层侦听,只能侦听至第一层,如果希望侦听对象里得到属性需要配置深度侦听。

                示例:

<script setup>
import{ref,watch} from 'vue'
const state = ref(
  {
    count:0,
    id:0,
    name:'gaga'
  })
const setState = ()=>{
  state.value.count++
}
const setStateName = () =>{
  state.value.name ='haha'
}
watch(state,()=>{
  console.log("state的count发生变化了")
},{
   deep:true
})
</script>
<template>
  <button @click="setState">{{ state.count }}</button>
  <button @click="setStateName">{{ state.name }}</button>
</template>
<style scoped>
</style>

        4.精确侦听  如果对象中有多个属性,希望只侦听某一个属性需要配置精确侦听。

                示例:

<script setup>
import{ref,watch} from 'vue'
const state = ref(
  {
    count:0,
    id:0,
    name:'xiaoming'
  })
const setState = ()=>{
 state.value.count++
}
const setStateName = () =>{
 state.value.name ='daming'
}
watch(()=>state.value.count,()=>{
 console.log("侦听了state对象的count属性")
})
</script>
<template>
 <button @click="setState">{{ state.count }}</button>
 <button @click="setStateName">{{ state.name }}</button>
</template>
<style scoped>
</style>

Vue3中组合API:生命周期函数(钩子函数)

组件从创造到销毁的过程称为生命周期,可以利用生命周期函数在创造或销毁过程中进行逻辑操作

setup  onMounted加载时  onUnmounted组件卸载时  onBeforeUpdate更新前  onUpdate更新后

                示例:

<script setup>
import { onMounted } from 'vue';
onMounted(()=>{
  console.log("调用了onMounted函数1")
})
onMounted(()=>{
  console.log("调用了onMounted函数2")
})
onMounted(()=>{
  console.log("调用了onMounted函数3")
})
</script>
<template>
</template>
<style scoped>
</style>
Vue 3的组合API是一种新的编程模式,它使得在Vue组件可以更灵活地组织和复用逻辑。下面是对Vue 3组合API的介绍: 1. Composition API组合API):Vue 3引入了Composition API,它允许我们将逻辑按照功能进行组合,而不是按照生命周期钩子进行划分。这样可以更好地组织和复用代码。 2. setup函数:在Vue 3,我们需要在组件使用setup函数来定义组合API。setup函数在组件创建之前执行,并且接收两个参数:props和context。我们可以在setup函数定义响应式数据、计算属性、方法等。 3. reactive函数:reactive函数是Vue 3用来创建响应式数据的函数。我们可以使用reactive函数将普通对象转换为响应式对象,从而实现数据的双向绑定。 4. ref函数:ref函数是Vue 3用来创建单个响应式数据的函数。与reactive函数不同,ref函数返回一个包装过的对象,我们需要通过.value属性来访问和修改数据。 5. computed函数:computed函数用来创建计算属性。与Vue 2的计算属性类似,我们可以使用computed函数来定义一个依赖其他响应式数据的属性。 6. watch函数:watch函数用来监听响应式数据的变化。我们可以使用watch函数来执行一些副作用操作,比如发送网络请求或者更新DOM。 7. 生命周期钩子:在Vue 3,生命周期钩子函数被废弃了,取而代之的是使用setup函数来处理组件的生命周期逻辑。我们可以在setup函数使用onMounted、onUpdated等函数来模拟Vue 2的生命周期钩子。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值