Open sleepyShen1989 opened 1 year ago
<Transition>
传入一个 mode属性mode="out-in"
来实现这个行为 (组件/元素切换)animation
和transition
, 此时你需要显式地传入 type 属性来声明,告诉 Vue 需要关心哪种类型(两个动画时长不同,一般选时间较长的那个):css="false"
属性
// demo:对ref值变化进行防抖处理
import { customRef } from 'vue'
export default function (value, delay = 500) {
let timer = null
return customRef((track, trigger)=>{
return {
get() {
track();
return value
},
set(newValue) {
clearTimeout(timer)
timer = setTimeout(()=>{
value = newValue;
trigger()
}, delay)
}
}
})
}
在侦听函数中执行网络请求,在网络请求还没达到的时候,我们停止了侦听器或者侦听函数被再次执行,那么上一次的网络请求应该被取消
// watchEffect传入的函数的参数
const stopWatch = watchEffect((onInvalidate)=>{
console.log('watchEffect执行')
const timer = setTimeout(()=>{
console.log('模拟网络请求')
})
onInvalidate(()=>{
clearTimeout(timer)
})
})
// dom挂载完再执行
<div ref="title">123</div>
const title = ref(null)
watchEffect(()=>{
console.log(title.value)
},{
flush: "post"
})
通过provide提供可变数据时,记得使用readonly包裹提供的数据,防止其被子组件修改
import { provide, ref, readonly } from 'vue'
const name = ref('abc')
provide('name', readonly(name))
Vue3文档备忘
工具链
性能优化
shallowRef()
和shallowReactive()
来绕开深度响应v-model
另一种在组件内实现 v-model 的方式
与外部状态系统集成
将外部状态放在一个
shallowRef
中。一个浅层的 ref 中只有它的.value
属性本身被访问时才是有响应性的,而不关心它内部的值。当外部状态改变时,替换此 ref 的 .value 才会触发更新不可变数据
immer
状态机:
XState
RxJS
RxJS @vueuse/rxjs