小程序默認是不支持html格式的內(nèi)容顯示的,那我們需要顯示html內(nèi)容的時候,就可以通過wxParse來實現(xiàn)。
s1.下載wxparse(鏈接:https://pan.[baidu.com/s/1htmfVow]-(http://baidu.com/s/1htmfVow) 密碼:x56c)
s2.將其中的wxparse復制到小程序的項目中(與pages同級目錄)
s3.在app.wxss全局樣式文件中,需要引入wxParse的樣式表
@import "wxParse/wxParse.wxss";
- s4.在需要加載html內(nèi)容的頁面對應(yīng)的js文件里引入wxParse
let wxparse = require("../../wxParse/wxParse.js");
- s5.通過調(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對象,一般為this(必填)
5.imagePadding為當圖片自適應(yīng)是左右的單一padding(默認為0,可選)
data: {
dkheight:300,
dkcontent: "你好<br/>nihao, <br/><br/><br/><br/><br/><br/><br/>這個是測試<br/><br/>你同意了嗎<br/><br/><br/>hehe<b>n你好啊,我加粗了kk</b >",
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
// 獲得高度
let winPage = this;
wx.getSystemInfo({
success: function (res) {
let winHeight = res.windowHeight;
console.log(winHeight);
winPage.setData({
dkheight: winHeight - winHeight*0.05 - 80
})
}
})
wxparse.wxParse('dkcontent', 'html', this.data.dkcontent, this, 5);
},
- s6.在頁面中引用模板
<import src="../../wxParse/wxParse.wxml"/>
<view class='page'>
<view class='page__title dk-title'>顯示html格式</view>
<view class="page__bd page__bd_spacing">
<view class='dk-show' style='height:{{dkheight}}px;'>
<template is="wxParse" data="{{wxParseData:dkcontent.nodes}}"/>
</view>
</view>
</view>

1.jpg