vue3 immediate
时间: 2023-08-15 22:14:15 浏览: 123
Vue 3 introduces a new feature called "immediate" to the reactivity system. The "immediate" option allows you to specify whether a reactive effect should be triggered immediately or not. By default, when you create a reactive effect using the `watch` function or the composition API's `watchEffect`, the effect will not be triggered on the first render. However, if you pass `immediate: true` as an option, the effect will be triggered immediately upon creation.
Here's an example of how to use the "immediate" option with the `watch` function:
```javascript
import { ref, watch } from 'vue';
const count = ref(0);
watch(
count,
(newCount, oldCount) => {
console.log(`Count changed from ${oldCount} to ${newCount}`);
},
{ immediate: true }
);
```
In the above example, the effect function will be called immediately with the initial value of `count`, and subsequently whenever `count` changes.
This "immediate" option is useful in scenarios where you want to trigger an effect immediately upon creation, such as fetching initial data or performing an initial setup.
阅读全文