watch
,watch API
与选项式API this.$watch
(以及相应的watch
选项) 完全等效。watch
需要侦听特定的数据源,并在单独的回调函数中执行副作用。默认情况下,它也是惰性的——即回调仅在侦听源发生变化时被调用。
示例:
watch(
() => state.deptName,
(newVal, oldVal) => {
tree.value.filter(newVal);
console.log(oldVal);
},
{
//如果加了这个参数,值为true的话,就消除了惰性,watch会在创建后立即执行一次
immediate: true,
//这个参数代表监听对象时,可以监听深度嵌套的对象属性
deep: true,
}
);
defineProps() 和 defineEmits()
<script setup>
const props = defineProps({
foo: String
})
const emit = defineEmits(['change', 'delete'])
// setup 代码
</script>