Vue filter方法中的this
項目開發時使用filter過濾數組時,在方法塊里面不能使用this,否者會報undefined錯誤,經查閱過濾器的說用發現,是vue中的過濾器更偏向於對文本數據的轉化,不能夠一欄this上下文,所以如果需要使用到上下文的this,應該使用computed計算屬性或者method方法
const tableData = this.memberData.filter(function (item) {
return item.cetfId === this.cetfId // 輸出this.cetfId為 undefined
})
最簡單的方法:定義一個參數傳進去就好了
var cetfId=this.cetfId
const tableData = this.memberData.filter(function (item) {
return item.cetfId === cetfId
})