Vue 监听状态 watch 与监听状态 watchEffect

监听状态 watch

watch 函数用于监听响应式数据的变化。


使用 watch 函数监听基于 ref 创建的响应式数据 (基本数据类型)。

import { ref, watch } from "vue"
export default {
  setup() {
    const text = ref("")
    watch(text, (current, previous) => {
      console.log("current", current)
      console.log("previous", previous)
    })
    return { text }
  },
}
<template>
  <input type="text" v-model="text" />
</template>
<template>
  <input type="text" v-model="text" />
</template>

使用 watch 监听基于 ref 创建的响应式数据 (引用数据类型)。

import { ref, watch } from "vue";
​
export default {
  name: "App",
  setup() {
    const person = ref({ name: "张三" });
    watch(person.value, (current) => {
      console.log(current);
    });
  },
};
<template>
  <button @click="onClickHandler">{
  { person.name }}</button>
</template>

使用 watch 监听响应式数据内部的具体属性 (基本数据类型)

import { ref, watch } from "vue";
​
export default {
  name: "App",
  setup() {
    const person = ref({ name: "张三" });
    watch(
      () => person.value.name,
      (current) => {
        console.log(current);
      }
    );
    return { person };
  },
};

使用 watch 监听响应式数据内部的具体属性 (引用数据类型)

<template>
  <p>{
  { person.brand.title }} {
  { person.name }}</p>
  <button @click="changeBrandTitle">title</button>
  <button @click="changeName">name</button>
</template>
​
<script>
import { ref, watch } from "vue";
export default {
  name: "App",
  setup() {
    const person = ref({ brand: { title: "宝马" }, name: "张三" });
    const changeBrandTitle = () => {
      person.value.brand.title = "奔驰";
    };
    const changeName = () => {
      person.value.name = "李四";
    };
    watch(person.value.brand, (current) => {
      console.log(current);
    });
    return { person, changeBrandTitle, changeName };
  },
};
</script>

使用 watch 监听基于 reactive 创建的响应式数据。

import { reactive, watch } from "vue"
export default {
  setup() {
    const person = reactive({ name: "张三" })
    const onClickHandler = () => {
      person.name = "李四"
    }
    watch(person, (current, previous) => {
      console.log(current)
    })
    return { person, onClickHandler }
  },
}
<template>
  {
  { person.name }}
  <button @click="onClickHandler">button</button>
</template>

使用 watch 监听多个值的变化

import { ref, watch } from "vue"
export default {
  setup() {
    const firstName = ref("")
    const lastName = ref("")
    watch([firstName, lastName], current => {
      console.log(current)
    })
    return { firstName, lastName }
  },
}
<template>
  <input type="text" v-model="firstName" />
  <input type="text" v-model="lastName" />
</template>

使 watch 监听数据在初始时执行一次

import { ref, watch } from "vue"
export default {
  setup() {
    const firstName = ref("hello")
    const lastName = ref("world")
    watch(
      [firstName, lastName],
      current => {
        console.log(current)
      },
      {
        immediate: true,
      }
    )
    return { firstName, lastName }
  },
}

监听状态 watchEffect

watchEffect 和 watch 一样,都是用于监听响应式数据的变化。

区别(重点)

watchEffect 只关心数据的最新值,不关心旧值是什么,而且 watchEffect 默认会在初始时执行一次。

import { ref, watchEffect } from "vue";

export default {
  name: "App",
  setup() {
    const firstName = ref("");
    const lastName = ref("");
    watchEffect(() => {
      console.log(firstName.value);
      console.log(lastName.value);
    });
    return { firstName, lastName };
  },
};
<template>
  <input type="text" v-model="firstName" />
  <input type="text" v-model="lastName" />
</template>

相关推荐

  1. Vue 监听状态 watch 监听状态 watchEffect

    2023-12-29 09:12:01       35 阅读
  2. VUEwatch 监听失效

    2023-12-29 09:12:01       38 阅读
  3. vue watch监听

    2023-12-29 09:12:01       38 阅读
  4. vue3里的watch watchEffect

    2023-12-29 09:12:01       33 阅读
  5. vue3 watchEffect 监听子组件变化

    2023-12-29 09:12:01       35 阅读
  6. uniapp监听wifi连接状态

    2023-12-29 09:12:01       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-29 09:12:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-29 09:12:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-29 09:12:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-29 09:12:01       20 阅读

热门阅读

  1. Python学习笔记(三) 数据结构与常用方法

    2023-12-29 09:12:01       33 阅读
  2. 数据结构前言

    2023-12-29 09:12:01       40 阅读
  3. ubuntu开放防火墙端口

    2023-12-29 09:12:01       33 阅读
  4. 什么是DDOS 攻击?常见的DDOS攻击有哪些?

    2023-12-29 09:12:01       36 阅读
  5. linux卸载小皮面板phpstudy教程

    2023-12-29 09:12:01       43 阅读
  6. BCELoss,BCEWithLogitsLoss和CrossEntropyLoss

    2023-12-29 09:12:01       31 阅读