• vue 中的监听器watch


    图解

    在这里插入图片描述

    监听器的作用

    1. 开发中我们在data返回的对象中定义了数据,这个数据通过插值语法等方式绑定到templatez中;
    2. 当数据变化时,template会自动进行更新来显示最新的数据;
    3. 但是在某些情况下,我们希望在代码逻辑中监听某个数据的变化,这个时候就需要用监听器watch来完成了;

    vue 的data的watch

    案例:
    希望用户在input中输入一个问题;
    每当用户输入最新内容时,我们就获取最新内容,并且提交服务器;
    那么,我们就需要实时的去获取最新的数据变化;
    此时就要用到监听器watch去监听数据是否发生变化

    const app = Vue.createApp({
    	data(){
    		return{
    			message: 'Hello Vue'
    			info: {name:'yema', age:18}
    		}
    	},
    	methods:{
    		changeMessage(){
    			this.message = 'hello yema';
    			this.info = {name:'yema'};
    		}
    	},
    	watch:{
    		// 1.默认有两个参数 newValue/oldValue
    		message(newValue,oldValue){
    			console.log('message数据发生了变化',newValue,oldValue)
    		},
    		info(newValue,oldVlaue){
    			// 2.如果是对象类型,那么拿到的是代理对象
    			console.log('info数据发生了变化',newValue,oldValue)
    			console.log(newValue.name, oldValue.name)
    			// 3.获取原始数据
    			console.log({...newValue})
    		}
    	}
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    vue的watch监听器

    1、常见一个对象,赋值给info
    2、点击按钮的时候会修改info.name的值
    3、此时使用watch并不能监听info,因为默认情况下,watch只是在监听info的引用变化,对于内部属性的变化是不会做出响应的
    4、所以我们可以使用deep深度监听
    5、希望一开始就会立即执行一次:这个时候我们使用immediate选项;无论数据是否变化,监听的函数都会有限执行一次

    <div id='app'>
    	<h2>{{info.name}}</h2>
    	<button @click="changeInfo">修改info</button>
    </div>
    
    • 1
    • 2
    • 3
    • 4
    const app = Vue.createApp({
    	data(){
    		return{
    			info: {name:'yema', age:18}
    		}
    	}
    	methods:{
    		changeInfo(){
    			// 创建一个对象,赋值给info
    			this.info = {name:'yema'}
    			// 直接修改对象里的一个属性
    			this.info.name = 'yema'
    		}
    	},
    	watch:{
    		// 默认watch监听不会进行深度监听
    		info(newValue, oldValue){
    			console.log('监听到info变化',newValue,oldValue)
    		},
    		// 进行深度监听
    		info:{
    			handler(newValue,oldValue){
    				console.log('监听到info变化',newValue,oldValue)
    				console.log(newValue === oldValue)
    			}
    
    			// 监听器选项
    			// info 进行深度监听
    			deep: true
    
    			// 第一次渲染直接执行一次监听器
    			immediate: true
    		}
    	},
    	'info.name': function(newValue,oldValue){
    		console.log('name发生改变',newValue,oldValue);
    	}
    })
    
    app.mount("#app");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    『无为则无心』Python日志 — 64、Python日志模块logging介绍
    信号槽连接失败原因分析
    react中的this指向问题
    《Real-Time Rendering 3rd》读书笔记
    Redis安装及使用(Windows&&Linux)
    在Linux中nacos集群模式部署
    猿创征文|『单片机原理』程序存储器的结构
    10-Spring架构源码分析-IoC 之解析 bean标签:BeanDefinition
    猿创征文 第二季| #「笔耕不辍」--生命不息,写作不止#
    Metabase学习教程:系统管理-6
  • 原文地址:https://blog.csdn.net/qq_46177396/article/details/127863440