我有一个带有以下哈希值的组件
{
computed: {
isUserID: {
get: function(){
return this.userId?
}
}
}
我应该关注更改的是isUserID
还是userId
? 你能监视计算属性吗?
我有一个带有以下哈希值的组件
{
computed: {
isUserID: {
get: function(){
return this.userId?
}
}
}
我应该关注更改的是isUserID
还是userId
? 你能监视计算属性吗?
watch:{ myComputedProperty : { handler(newVal){ //当变化发生时执行某些操作}, deep: true}}
- Cindy Conwaycomputed: {
name: {
get: function(){
return this.name;
}
}
},
watch: {
name: function(){
console.log('changed');
}
}
这样,我们就能观察计算属性是否发生变化,并在控制台上得到通知。
以下是使用Vue 3的Composition API实现的方法:
<script setup>
import { ref, computed, watch } from 'vue'
const variable = ref(1)
const computedProperty = computed(() => {
return variable.value * 2
})
watch(computedProperty, (newValue, oldValue) => {
console.log('computedProperty was ' + oldValue + '. Now it is ' + newValue + '.')
})
</script>
<template>
<button @click="variable++">Click me</button>
</template>
this.ID
时,同时设置this.userId
,我不确定这样会起作用。 - Saurabh