watch
在watch是可以监听属性的变化的
watch: {
starttime(newName, oldName) {
console.log(newName, oldName)
if (newName !== oldName) {
this.getgroupamode()
this.getgroupanature()
this.getgroupaindustry()
this.getsavings()
},
immediate:true //第一次刷新页面时就会执行
},
},
starttime 是属性名
可以监听当前组件data数据和prop传过来的属性值,当发生变化,可以执行一些操作
有两个属性值
immediate:组件加载立即触发回调函数执行,
deep: 深度监听,为了发现对象内部值的变化,复杂类型的数据时使用,例如数组中的对象内容的改变,注意监听数组的变动不需要这么做。
注意:deep无法监听到数组的变动和对象的新增,参考vue数组变异,只有以响应式的方式触发才会被监听到。
另外的一种格式
字符串格式
watch:{
"starttimt":function(){
console.log("执行相应的操作")
}
}
computed
支持缓存,只有依赖的数据发生改变才会执行
不支持异步,如果有异步操作是不会执行的
默认走缓存,也是和watch一样,当前组件data属性值和prop,父组件传递给子组件的值
在computed 属性为函数,默认执行get和set的方法,如果值发生改变会走set的方法
computed:{
satrtime(){
return this.fristTime+" "+this.endTime
}
}