天天看点

vue中过滤器filters的this指向问题。

今天在完成一个时间戳转化时想试试vue的过滤器filters,代码如下:

execute_time(time) {
      if (time === 0) return '/'
      else return this.getTime(time)
    }
           

我发现报了个"TypeError: Cannot read property ‘getTime’ of undefined",我第一时间就打印了this,果然是undefined。在查阅了尤大大的对这个过滤器filters的说明我才发现:vue中的过滤器更偏向于对文本数据的转化,而不能依赖this上下文,如果需要使用到上下文this我们应该使用computed计算属性的或者一个method方法。

vue中过滤器filters的this指向问题。

这里查看 有关

this

undefined in filters 的 issue