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