指令 directive
- Vue官网解释 自定义指令 | directive
1. 随输入搜索结果
<script setup>
// 在模板中启用 v-inputSearch
import { debounce } from "lodash";
const vInputSearch = {
mounted: (el, binding) => {
el.addEventListener('input',debounce(async ()=>{
consolelog("input value",el.value);
const result = await http.post({ text: el.value});
binding.value(result);
}, 500));
}
}
function inputSearchRes(res) {
// res : 这个就是返回的 结果
//... 处理数据,然后赋值。
}
</script>
<template>
<input v-inputSearch="inputSearchRes" />
</template>