1. 程式人生 > >vue監聽多個變量的方法

vue監聽多個變量的方法

control val param query als pre log 變量 tor

vue當中有時需要監聽多個變量,方法如下,推薦方法一

一、把多個變量放在一個對象裏

data() {
    return {   
      switchParam: {
        a: false,
        b: false,
        c: false
      }
    }
}

watch: {  
    switchParam: {
      deep: true,
      handler(newVal) {
        let dom = document.querySelector(‘.vis-panels-controler‘);
        
if (newVal.a || newVal.b || newVal.c) { dom.style.zIndex = 180; } else { dom.style.zIndex = 120; } } }, }

二、小技巧方法

data() {
    return {
      a: false, // 自主分析
      b: false,
      c: false
    }
  },
  computed: {   
   allPanelShow() {
    this.a;
    
this.b; this.c;
  return Date.now() } }, watch: { allPanelShow() { let dom = document.querySelector(‘.vis-panels-controler‘); if (this.a || this.b || this.c) { dom.style.zIndex = 180; } else { dom.style.zIndex = 120; } }, }

vue監聽多個變量的方法