可以从特定值获取观察程序中的密钥吗?
例如,如果我有这个对象
data() {
return {
form: {
a: 'A',
b: 'B'
}
}
}
如果a的值更新,我想获取他的密钥。
watch: {
'form' (val) : {
//get the key
}
}
我知道我可以通过这种方式观察特定值:'form.a'(val) {}
但出于某种目的,我需要从对象中获取键。
谢谢
PS:对不起,英语不好。
试试这个
watch: {
form: {
handler(newVal, oldVal) {
let relevant = newVal.a
//whatever you needed to do to that
},
deep: true
}
}
或者,您可以使用Object.keys(val)
获取form
数据对象属性键,如下所示:
watch: {
'form' (val) : {
let keys= Object.keys(val)// gives ["a", "b"]
// and do whatever you want with that keys
}
}