今天在完成一个时间戳转化时想试试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方法。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLzkDN1EjMwETMwETOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
这里查看 有关
this
undefined in filters 的 issue