Vue3 setup() 中使用 watch 监听 a-select 变化并输出结果
在 Vue 3 的setup()函数中,可以使用watch()函数来监听a-select的变化,并将结果输出给控制台。可以通过onUpdate回调函数来监听v-model的变化。
以下是示例代码:
import { ref, watch } from 'vue';
setup() {
const yncf = {
cfDoctor: ref(null) // 使用ref()创建响应式数据
};
// 监听cfDoctor的变化
watch(yncf.cfDoctor, (newVal) => {
console.log(newVal);
});
return {
yncf,
doctors: [] // 假设有一个doctors数组
};
}
在上面的示例中,我们使用ref()函数来创建一个响应式数据cfDoctor。然后使用watch()函数来监听cfDoctor的变化,并在变化时将新的值输出到控制台。
注意,watch()函数的第一个参数可以是一个ref对象,也可以是一个返回ref对象的函数。在这个示例中,我们直接传递了yncf.cfDoctor作为第一个参数。
另外,需要确保在模板中绑定a-select的v-model指令到yncf.cfDoctor,以便将a-select的值绑定到cfDoctor变量。
<a-select v-model:value='yncf.cfDoctor' show-search :filter-option='changeOption' style='width: 150px'>
<a-select-option v-for='item in doctors' :key='item.pym' :value='item.id'> {{ item.employname }}</a-select-option>
</a-select>
当a-select的值发生变化时,cfDoctor的值也会相应地更新,并触发watch()函数中的回调函数,将新的值输出到控制台。
原文地址: https://www.cveoy.top/t/topic/qwKe 著作权归作者所有。请勿转载和采集!