直接上代碼
小程序頁面
<rich-text type="text" nodes={{content}}></rich-text>
數據層
data(){
return {content:""},
},
methods:{
getHtml(){
wx.request({
url:api+"api/mobile/homepage.php?choice=2&id=2",
method:"get",
dataType:'json',
success: (res) => {
// 返回的json數據
console.log(res.data.content);
this.content=this.escape2Html
(res.data.content)
},
});
},
//解析html
escape2Html: function (str) {
var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; });
},
}