Datos de monitoreo de atributos de vigilancia de instancias de Vue

Directorio de artículos

efecto

El atributo de reloj de Vue se puede usar para monitorear cambios en el valor especificado en los datos

gramática

var vm=new Vue({
    
    
				el:'#app',
				data:{
    
    
					first:'',
					next:'',
					result:''
				},
				methods:{
    
    },
				watch:{
    
    
					// 方法名就是指定要监听的data属性名,方法提供了两个值,newVal和oldVal故名思意
					// 即是改变之后的新值,和改变之前老值
					'first':function(newVal,oldVal){
    
    
						console.log('文本框first值发生变化  改变的新值:'+newVal+'  之前的老值:'+oldVal)
					},
					'next':function(newVal,oldVal){
    
    
						console.log('文本框first值发生变化   改变的新值:'+newVal+'  之前的老值:'+oldVal)
					}
				}
			})

Caso

<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
		<title>Bootstrap 101 Template</title>
		<script src="../js/Vue/Vue2.6.11.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="app">
			<input type="text" name="" v-model="first" />+
			<input type="text" name="" v-model="next" />=
			<input type="text" name="" v-model="result" />
		</div>
		
		<script type="text/javascript">
			var vm=new Vue({
     
     
				el:'#app',
				data:{
     
     
					first:'',
					next:'',
					result:''
				},
				methods:{
     
     },
				watch:{
     
     
					'first':function(newVal,oldVal){
     
     
						console.log('文本框first值发生变化  改变的新值:'+newVal+'  之前的老值:'+oldVal)
					},
					'next':function(newVal,oldVal){
     
     
						console.log('文本框first值发生变化   改变的新值:'+newVal+'  之前的老值:'+oldVal)
					}
				}
			})
		</script>
	</body>
</html>

Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/qq_42418169/article/details/108866176
Recomendado
Clasificación