最近在渲染圖檔時遇到了一個問題,頁面上隻需要使用一張圖檔作為封面,而背景傳回的是字元串形式的圖檔組,于是準備用filters去篩選一下,提取圖檔組的第一張圖
filters:{
filtertu:function(res){
communityimg=res.split(",")[0]
console.log(communityimg);
return communityimg;
}
},
結果程式就報錯了
當時就很困惑,split()怎麼會報錯呢,嘗試了幾種方法都無果後,我看到有人說可能是它在某個狀态下值為undefined,于是我測試了一下值為undefined時的結果
var str=undefined
console.log(str.split(","))
然後程式報錯
于是我将源程式改成了
filters:{
filtertu:function(res){
communityimg=(res||'').split(",")[0]
console.log(communityimg);
return communityimg;
}
},
果然不報錯了~~