最近遇到一个坑:
后台配置了富文本html代码,微信小程序中解析有各种问题:
- taro自带的html解析,效果差,图片出不来
- 微信rich-text组件,效果好,但是无法给图片绑定点击预览事件,也无法长按识别保存等。
找来找去。找到一个库
https://github.com/jin-yufeng/mp-html
由于我使用的是taro+react版,参考第三方插件的使用方法如下。成功填坑
使用方法
原生平台
npm
方式在项目目录下安装组件包
1
npm install mp-html
开发者工具中勾选
使用 npm 模块
并点击工具 - 构建 npm
在需要使用页面的
json
文件中添加1
2
3
4
5{
"usingComponents": {
"mp-html": "mp-html"
}
}在需要使用页面的
wxml
文件中添加1
<mp-html content="{{html}}" />
在需要使用页面的
js
文件中添加1
2
3
4
5
6
7Page({
onLoad () {
this.setData({
html: '<div>Hello World!</div>'
})
}
})
源码方式
将源码中对应平台的代码包(
dist/platform
)拷贝到components
目录下,更名为mp-html
在需要使用页面的
json
文件中添加1
2
3
4
5{
"usingComponents": {
"mp-html": "/components/mp-html/index"
}
}
后续步骤同上
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小黑的小站!