customRef的两个参数分别是用于追踪的 track 与用于触发响应的 trigger
<template>
<input type="text" v-model="keyword" />
<h3>{{ keyword }}</h3>
</template>
<script>
import { customRef } from "vue";
export default {
setup() {
function aaa(value) {
console.log("value", value);
return customRef((track, trigger) => {
return {
get() {
console.log("拿到了");
track(); //追踪数据
return value;
},
set(newvalue) {
console.log("newvalue", newvalue);
value = newvalue;
trigger(); //通知vue去重新解析模板
},
};
});
}
let keyword = aaa("hello");
return {
keyword,
};
},
};
</script>
<style>
</style>