今天在完成一個時間戳轉化時想試試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方法。
這裡檢視 有關
this
undefined in filters 的 issue