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

作者: 独西楼Q | 来源:发表于2019-11-18 11:43 被阅读0次

项目中有显示后台传过来富文本前端显示的功能,因为微信小程序没有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);

相关文章

网友评论

    本文标题:微信小程序解析html,wxParse的使用

    本文链接:https://www.haomeiwen.com/subject/eppvictx.html