1.main.js
Vue.prototype.$praseStrEmpty = function(str) {
if (typeof str === 'undefined' || str === null) {
return "-";
}
return str;
}
2.DOM
<div>{{$praseStrEmpty(需要转换的字符:如item.str)}}</div>
3.Js里面调用
var newStr = this.$praseStrEmpty(str)
但是如过数据结构多层 在中间为null 该如何处理?
//数据结构
// 1.正常数据结构是没问题的
constructionTimeA:{
constructionTimeB:{
constructionTimeC:'2023-7-26 09:40:43'
}
}
<span>{{$praseStrEmpty(constructionTimeA.constructionTimeB.constructionTimeC)}}</span>
// 2.如果constructionTimeB为null 该如何处理呢?
constructionTimeA:{
constructionTimeB:null
}
<span>{{$praseStrEmpty(constructionTimeA.constructionTimeB.constructionTimeC)}}</span> //页面卡死