Vue 中有一个方法 watch,它是用来监测Vue实例上的数据变动。
下面展示一个 demo片段。
export default { data() { return { imgUrl:'', flag:'', showBtn:false, } }, watch:{ flag(newFlag,oldFlag){ if(this.flag != ""){ this.showBtn = true; }else{ this.showBtn = false; } } },下面展示一个 demo片段。
export default { data() { return { carModel: '', } }, watch:{ "$store.state.carModel":{ deep:true, handler:function (newValue,oldValue) { this.carModel = newValue; } }, },两种写法仅供参考