天天看點

小程式富文本解析

項目中有很多地方需要解析背景填寫的富文本内容,小程式提供了富文本解析的元件 rich-text ,但是有很多限制。(https://developers.weixin.qq.com/miniprogram/dev/component/rich-text.html)

小程式富文本解析
小程式富文本解析

是以找到了wxParse插件解析html

使用方法:

1.在github下載下傳   (https://github.com/icindy/wxParse/tree/master/wxParse)

小程式富文本解析

2.複制檔案到小程式中

小程式富文本解析

3.使用

(1)所需頁面引入wxParse.wxml

<import src="/wxParse/wxParse.wxml" />
           

(2)對應頁面引入wxParse.wxss

@import '/wxParse/wxParse.wxss';
           

(3)對應頁面的js中引入wxParse.js

const WxParse = require('../../../wxParse/wxParse.js');
           

(4)使用template元件

在引入wxParse.wxml的頁面中插入元件,插入的位置即需要富文本解析顯示的地方

<import src="/wxParse/wxParse.wxml" />
<view class='content'>
  <template is='wxParse' data='{{wxParseData:article.nodes}}' />
</view>
           

(5)在js檔案中使用WxParse中的方法進行富文本解析

const WxParse = require('../../../wxParse/wxParse.js');
Page({

    data:{},
    onLoad:function(){
        let that=this;
        wx.request({
            url:'',
            data:{},
            method:'POST',
            header:{
                'content-type': 'application/x-www-form-urlencoded'
            },
            success(res){
                let str=res.data.content;
                // WxParse.wxParse(模闆名稱,轉換的格式,資料,目前對象,設定padding值 預設0)
                WxParse.wxParse('article','html',str,that,5); 
            }
        })
    }

})
           

繼續閱讀