微信小程序解析html,wxParse的使用

项目中有显示后台传过来富文本前端显示的功能,因为微信小程序没有html语法,用了wxParse组件解析HTML

wxParse githubd地址: https://github.com/icindy/wxParsewxParse的源文件和使用方法

image.png

如果用rich-text,结果就是这样
image.png

前端显示:


image.png

wxParse的应用

1.拷贝 wxParse 文件夹,放在项目文件夹下

image.png

2.引用 wxParse

//在使用的Wxss中引入WxParse.css,可以在app.wxss
@import "/wxParse/wxParse.wxss";
//在使用的js中引入WxParse模块
const WxParse = require('../../wxParse/wxParse.js');
// 在使用的wxml引入模板
<import src="你的路径/wxParse/wxParse.wxml"/>
//这里data中article为bindName
<template is="wxParse" data="{{wxParseData:article.nodes}}"/>

3.数据绑定

  • WxParse.wxParse(bindName , type, data, target, imagePadding)
  • 1.bindName绑定的数据名(必填)
  • 2.type可以为html或者md(必填)
  • 3.data为传入的具体数据(必填)
  • 4.target为Page对象,一般为this(必填)
  • 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
var article = '<div>我是HTML代码</div>';

var that = this;
WxParse.wxParse('article', 'html', article, that, 5);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容