vue 中使用 watch 的各種問題


 報錯: Method "watch" has type "object" in the component definition. Did you reference the function Method "watch" has type "object" in the component definition. Did you reference the function correctly?

  原因: watch 是一個對象,應該以鍵值對的形式來使用,但是我 將 watch 放到了 methods: {} 中,導致了這個問題;

  解決辦法:  將watch 對象 拿出來,與 methods 平級;

 

附上當時的代碼:

watch: {
        "$route.path":function(newval){
          if(newval === '/home'){
            this.flag = false
          }else{
            this.flag = true
          }
        }
      }
  }
在 vue 組件的函數里面調用 定時器 setTimeout(function(){},1000) 和 setInterval(fucntiong(){},1000) 時, 必須使用箭頭函數, 因為定時器函數里面的 this 指向的是 window , 從而導致在 函數里面 用 this 調用 data  里面的數據時失敗,無法獲取; 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM