computed:不需要在data(){return {}}里面定义res,res的值受computed res(){}里面return 的值影响
<template>
<div>{{res}}</div>
</template>
<script>
export default{
data(){
return {
}
}
computed: {
res() {
return 10+5
}
}
}
</script>
watch:需要在data(){return {}}里面定义res(即被监听的属性),watch是通过被监听的属性值发生变化从而去执行某些动作,比如说影响其它属性的值,或执行某些方法
<template>
<div>
<input v-model='res'>{{to}}
</div>
</template>
<script>
export default{
data(){
return {
res:'23',
to:'',
obj:{},
}
}
watch: {
res(val) {
this.to = res
},
obj:{
handle(newVal,oldVal){
console.log(newVal)
console.log(oldVal)
},
deep:true //深度监听,当父子组件传值监听时,最好加上,确定能监听到从父组件传来的对象或者数组的值。
}
}
}
</script>