首页 > 其他分享 >computed,watch,watchEffect

computed,watch,watchEffect

时间:2022-11-01 18:11:41浏览次数:53  
标签:const computed watch value watchEffect message ref

computed

import { computed, ref } from "vue";

const firstName = ref("");
const lastName = ref("");

const name = computed(() => firstName.value + lastName.value);
// name.value = 'list' // 只读,不可修改
const name1 = computed(
  {
    get() {
      return firstName.value + lastName.value;
    },
    set(value) {
      firstName.value = firstName.value + value;
    },
  },
  {
    onTrack(e) {
      debugger; //调式
    },
    onTrigger(e) {
      debugger; //调式
    },
  }
);
name1.value = "你好,";

watch与watchEffect

import  reactive, ref, watch, watchEffect } from "vue";

const message = ref("");
const message2 = ref("");
watch(message, (newVal, oldValue) => {
  //.....
});
watch([message, message2], (newVal, oldValue) => {
  //newVal = []
  //olValue = []
});
const obj = reactive({ name: "", age: 18 });
watch(obj, () => {}, { deep: true, immediate: true });
watch(
  () => obj.age,
  () => {}
);

const stop = watchEffect(
  (onCleanup) => {
    // 默认第一次执行
    console.log("message", message.value);
    console.log("message2", message2.value);
    onCleanup(() => {
      //.. 在监听之前执行, 不会默认第一次执行
    });
  },
  {
    flush: "post", // 在dom渲染后执行, post, pre, sync
    onTrack() {}, // 调试
    onTrigger() {}, // 调试
  }
);
stop(); // 停止监听

标签:const,computed,watch,value,watchEffect,message,ref
From: https://www.cnblogs.com/JunLan/p/16848680.html

相关文章

  • vue中wahch和computed区别
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><metahttp-e......
  • 计算属性(computed)以及getset使用
    <!DOCTYPEhtml><html> <head> <metacharset="utf-8"> <title></title> <scripttype="text/javascript"src="./vue.js"></script> <!-- 计算属性(computed) 定义:......
  • 深度监视(watch)
    <!DOCTYPEhtml><html> <head> <metacharset="utf-8"> <title></title> <!-- 1.深度监视: 1.vue中的watch默认不检测对象内部值的改变 2.配置deep:true可以监......
  • 【Vue3】computed 计算属性
    computed计算属性计算属性就是当依赖的属性的值发生变化的时候,才会触发他的更改,如果依赖的值,不发生变化的时候,使用的是缓存中的属性值。Vue3中有两种写法如果计算......
  • 【Vue3】watch 监听器的使用
    watch的三个参数watch第一个参数,监听源watch第二个参数,回调函数cb(newVal,oldVal)watch第三个参数,一个options配置项是一个对象{immediate:true//是否......
  • Vue学习笔记之使用computed计算属性
    Vuecomputed0x00概述本文主要记录Vue页面使用computed计算属性0x01computed基本使用在computed中定义一个函数(看起来是一个函数,其实是一个属性),命......
  • 【转】Vue Watch 侦听器
    npmiaxios-S         ......
  • 说说Vue响应式系统中的Watcher和Dep的关系-面试进阶
    引言在这里我先提出两个问题(文章末尾会进行解答):在Vue的数据响应系统中,Dep和Watcher各自分担什么任务?Vue的数据响应系统的核心是Object.defineproperty一定是最好的吗?有......
  • vue 中watch 回调函数和methos中方法的区别
    1.vue中代码<el-inputv-model="searchkey"placeholder="键名"@input="handleInput"clearablestyle="max-width:300px;"></el-/utils/commonwen文件加下export......
  • CF580E - Kefa and Watch 线段树维护哈希
    题目思路区间修改+区间查询,考虑用线段树维护哈希实现。那么首先,需要明确判断循环节的方式:如上图所示是一个重要的结论:当区间的哈希值与的哈希值相等时,那么该区间是以为循环......