微信小程序使用wxParse解析html的實(shí)現(xiàn)示例
最近項(xiàng)目上遇到在微信小程序里需要顯示新聞內(nèi)容,新聞內(nèi)容是通過接口讀取的服務(wù)器中的富文本內(nèi)容,是html格式的,小程序默認(rèn)是不支持html格式的內(nèi)容顯示的,那我們需要顯示html內(nèi)容的時(shí)候,就可以通過wxParse來實(shí)現(xiàn)。
首先我們?cè)趃ithub上下載wxParse
https://github.com/icindy/wxParse
下載完之后我們需要用到目錄下的wxParse文件夾,把他拷貝到我們的項(xiàng)目目錄下
下面是具體的使用步驟
1.在app.wxss全局樣式文件中,需要引入wxParse的樣式表
@import "/page/wxParse/wxParse.wxss";
2.在需要加載html內(nèi)容的頁(yè)面對(duì)應(yīng)的js文件里引入wxParse
var WxParse = require('../../wxParse/wxParse.js');
3.通過調(diào)用WxParse.wxParse方法來設(shè)置html內(nèi)容
/** * WxParse.wxParse(bindName , type, data, target,imagePadding) * 1.bindName綁定的數(shù)據(jù)名(必填) * 2.type可以為html或者md(必填) * 3.data為傳入的具體數(shù)據(jù)(必填) * 4.target為Page對(duì)象,一般為this(必填) * 5.imagePadding為當(dāng)圖片自適應(yīng)是左右的單一padding(默認(rèn)為0,可選) */ Page({ data: { }, onLoad: function () { var that = this; wx.request({ url: '', method: 'POST', data: { 'id':13 }, header: { 'content-type': 'application/json' }, success: function(res) { var article = res.data[0].post; WxParse.wxParse('article', 'html', article, that,5); } }) } })
4.在頁(yè)面中引用模板
<import src="../../wxParse/wxParse.wxml"/> <template is="wxParse" data="{{wxParseData:article.nodes}}"/>
這樣就可以在微信小程序中嵌入html內(nèi)容了
wxParse多數(shù)據(jù)循環(huán)使用方法
方法介紹
/** * WxParse.wxParseTemArray(temArrayName,bindNameReg,total,that) * 1.temArrayName: 為你調(diào)用時(shí)的數(shù)組名稱 * 3.bindNameReg為循環(huán)的共同體 如綁定為reply1,reply2...則bindNameReg = 'reply' * 3.total為reply的個(gè)數(shù) */ var that = this; WxParse.wxParseTemArray("replyTemArray",'reply', replyArr.length, that)
使用方式
循環(huán)綁定數(shù)據(jù)
var replyHtml0 = `<div style="margin-top:10px;height:50px;"><p class="reply"> wxParse回復(fù)0:不錯(cuò),喜歡[03][04] </p> </div>`; var replyHtml1 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回復(fù)1:不錯(cuò),喜歡[03][04] </p> </div>`; var replyHtml2 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回復(fù)2:不錯(cuò),喜歡[05][07] </p> </div>`; var replyHtml3 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回復(fù)3:不錯(cuò),喜歡[06][08] </p> </div>`; var replyHtml4 = `<div style="margin-top:10px; height:50px;"> <p class="reply"> wxParse回復(fù)4:不錯(cuò),喜歡[09][08] </p> </div>`; var replyHtml5 = `<div style="margin-top:10px;height:50px;"> <p class="reply"> wxParse回復(fù)5:不錯(cuò),喜歡[07][08] </p> </div>`; var replyArr = []; replyArr.push(replyHtml0); replyArr.push(replyHtml1); replyArr.push(replyHtml2); replyArr.push(replyHtml3); replyArr.push(replyHtml4); replyArr.push(replyHtml5); for (let i = 0; i < replyArr.length; i++) { WxParse.wxParse('reply' + i, 'html', replyArr[i], that); if (i === replyArr.length - 1) { WxParse.wxParseTemArray("replyTemArray",'reply', replyArr.length, that) } }
模版使用
<block wx:for="{{replyTemArray}}" wx:key=""> 回復(fù){{index}}:<template is="wxParse" data="{{wxParseData:item}}"/> </block>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript Date對(duì)象詳解及時(shí)間戳和時(shí)間的相互轉(zhuǎn)換問題
這篇文章主要介紹了JavaScript Date對(duì)象詳解及時(shí)間戳和時(shí)間的相互轉(zhuǎn)換問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01Javascript 獲取字符串字節(jié)數(shù)的多種方法
Javascript 字符串字節(jié)數(shù)獲取功能多種方法2009-06-06javascript中encodeURI和decodeURI方法使用介紹
encodeURI和decodeURI是成對(duì)來使用的,因?yàn)闉g覽器的地址欄有中文字符的話,可以會(huì)出現(xiàn)不可預(yù)期的錯(cuò)誤,所以可以encodeURI把非英文字符轉(zhuǎn)化為英文編碼,decodeURI可以用來把字符還原回來2013-05-05在Layui中操作數(shù)據(jù)表格,給指定單元格添加事件示例
今天小編就為大家分享一篇在Layui中操作數(shù)據(jù)表格,給指定單元格添加事件示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10