vue3中watch和watchEffect使用实例分析

发布时间:2022-08-10 17:27:46 作者:iii
来源:亿速云 阅读:161

本文小编为大家详细介绍“vue3中watch和watchEffect使用实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“vue3中watch和watchEffect使用实例分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

watch

watch监听单个数据

<template>
    <input type="text" v-model="text1" />
</template>

<script setup>
import { ref, watch } from 'vue'
const text1 = ref('')

watch(text1, (newVal, oldVal) => {
    console.log('监听单个数据', newVal, oldVal)
})
</script>

watch监听多个数据

<template>
    <input type="text" v-model="text1" />
    <input type="text" v-model="text2" />
</template>

<script setup>
import { ref, watch } from 'vue'
const text1 = ref('')
const text2 = ref('')

watch([text1, text2], (newVal, oldVal) => {
    console.log('监听一组数据', newVal, oldVal)
})
</script>

watch监听对象

<template>
    name: <input type="text" v-model="student.name" />
    age: <input type="number" v-model="student.age" />
</template>
<script setup>
import { reactive, watch } from 'vue'
const student = reactive({
    name: '',
    age: ''
})
watch(student, (newVal, oldVal) => {
    console.log('newVal', newVal)
    console.log('oldVal', newVal)
})
</script>

 watch还有第三个参数,deepimmediate,可以加上看看效果

watch监听对象的某一个值

<template>
    name: <input type="text" v-model="student.name" />
    age: <input type="number" v-model="student.age" />
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'
const student = reactive({
    name: '',
    age: ''
})
watch(() => student.name, (newVal, oldVal) => {
    console.log('newVal', newVal)
    console.log('oldVal', newVal)
}, {
    deep: true,
    immediate: true
})
</script>

监听对象某一个属性的时候需要用箭头函数 

watchEffect

关于watchEffect,官网是这么介绍的:为了根据响应式状态自动应用和重新应用副作用,我们可以使用watchEffect方法,它立即执行传入的一个函数,同时响应式追踪其依赖,并在其依赖变更时重新运行该函数。 也就是说,我们并不需要传入一个特定的依赖源,而且它会立即执行一遍回调函数,如果函数产生了副作用,那它就会自动追踪副作用的依赖关系,自动分析出响应源。光看概念可能比较模糊,先来看个最简单的例子:

<template>
    name: <input type="text" v-model="student.name" />
    age: <input type="number" v-model="student.age" />
</template>

<script lang="ts" setup>
import { reactive, watchEffect } from 'vue'

const student = reactive({
    name: '',
    age: ''
})
watchEffect(() => {
    console.log('name: ',student.name, 'age: ', student.age)
})
</script>

watchEffect副作用

副作用,那什么是副作用呢,其实很简单,就是在监听之前,我得做一件事。

<template>
    name: <input type="text" v-model="student.name" />
    age: <input type="number" v-model="student.age" />

    <h3>{{student.name}}</h3>
</template>

<script lang="ts" setup>
import { reactive, watchEffect } from 'vue'

const student = reactive({
    name: '',
    age: ''
})
watchEffect((oninvalidate) => {
    oninvalidate(() => {
        student.name = '张三'
    })
    console.log('name: ', student.name)
}, {
    // pre 组件更新前; sync:强制效果始终同步; post:组件更新后执行
    flush: 'post'  // dom加载完毕后执行
})
</script>

监听之前让student.name赋值为'张三',无论你输入什么值,name一直都是'张三'

停止监听

我们用同步语句创建的监听器,会自动绑定到组件实例上,并且会在组件卸载时自动停止,但是,如果我们在异步回调里创建一个监听器,那它就不会绑定到当前组件上,必须手动去停止,防止内存泄漏。 那怎么去停止呢,其实我们只需要调用一下watchwatchEffect返回的函数

const stop = watchEffect(() => {})

// 停止监听
unwatch()

区别

用了一遍watchwatchEffect之后,发现他俩主要有以下几点区别:

读到这里,这篇“vue3中watch和watchEffect使用实例分析”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Vue3中watch如何使用
  2. vue3中watch和watchEffect怎么使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue3 watch watcheffect

上一篇:MySQL不适合构建索引及索引失效的情况有哪些

下一篇:Vuex3的状态管理实例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》