vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue 中使用 watch 报错

vue 中使用 watch 出现了如下的报错的原因分析

作者:独钓寒江雪

这篇文章主要介绍了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 中使用 watch 出现了如下的报错的原因分析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

您可能感兴趣的文章:
阅读全文