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?vue

  缘由: watch 是一个对象,应该以键值对的形式来使用,可是我 将 watch 放到了 methods: {} 中,致使了这个问题;函数

  解决办法:  将watch 对象 拿出来,与 methods 平级;this

 

附上当时的代码:spa

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  里面的数据时失败,没法获取; 
相关文章
相关标签/搜索