温馨提示×

vue如何监听变量

vue
养鱼的猫咪
1416
2021-05-12 08:52:46
栏目: 编程语言

在vue中监听变量的方法:1.新建项目,引入vue;2.定义监听对象;3.使用watch方法监听对象中的变量;

vue如何监听变量

具体步骤如下:

1.首先,新建一个html项目,并在项目中引入vue;

import Vue from 'vue'

2.引入vue后,在项目中定义一个监听对象;

data: {

a: 1,

b: 2,

c: 3,

d: 4,

e: {

f: {

g: 5

}

}

},

3.最后,监听对象定义好后,使用watch方法即可监听对象中的变量;

1)监听变量

watch: {

bet(newValue, oldValue) {

console.log(newValue);

}

}

2)监听某一个对象

watch: {

'bet.text': function (val, oldVal) {}

}

相关扩展:

1)使用service方法监听整个对象

serviceList:{

handler(){

console.log(this.serviceList);

},

deep:true,

immediate: true

},

0