<template>
<div>
<h1>hh</h1>
<input type="text" v-model="hello" />
<h1>{{ hello }}</h1>
</div>
</template>
<script setup>
import { ref, customRef } from "vue";
let myRef = (value) => {
let timer;
return customRef((track, trigger) => {
return {
get() {
track();//追踪value的变化
return value;
},
set(newValue) {
clearTimeout(timer);
timer = setTimeout(() => {
value = newValue;
trigger();//重新解析模板
}, 1000);
},
};
});
};
// let hello = ref('hello')
let hello = myRef("hello");
</script>
<style scoped></style>
自定义ref
最新推荐文章于 2024-06-09 20:17:27 发布