当前位置:网站首页>小程序editor富文本编辑使用及rich-text解析富文本
小程序editor富文本编辑使用及rich-text解析富文本
2022-07-17 05:05:00 【小青年一枚】
今天尝试了下在小程序中使用editor富文本编辑,然后再详情页使用rich-text来解析富文本html。
首先先下载editor组件,并放到项目目录中,(组件下载地址)

文件存放后,打开richText.wxml可以根据需求修改代码,richText.js同理。
然后在需要的json文件中引入
"usingComponents": {
"richText":"../../components/richText/richText"
},在需要的wxml中使用组件
<richText
id='richText'
readOnly='{
{readOnly}}'
placeholder='{
{placeholder}}'
formatDate='YY/MM/DD'
buttonTxt='保存'
bind:clearBeforeEvent='clearBeforeEvent'
bind:clearSuccess='clearSuccess'
bind:undo='undo'
bind:restore='restore'
bind:onEditorReady='onEditorReady'
bind:bindfocus='bindfocus'
bind:bindblur='bindblur'
bind:bindinput='bindinput'
bind:insertImageEvent='insertImageEvent'
bind:getEditorContent='getEditorContent'></richText>这里说下组件的方法及属性
readOnly 编辑器是否只读
clearBeforeEvent 清空编辑器内容之前的回调
clearSuccess 清空编辑器内容成功时回调
undo 撤销内容成功时回调
restore 恢复内容成功时回调
onEditorReady 编辑器初始化完成时回调,可以获取组件实例
bindfocus 编辑器聚焦时触发
bindblur 编辑器失去焦点时触发
bindinput 编辑器输入中时触发,实时返回富文本内容
insertImageEvent 插入图片按钮点击时回调
getEditorContent 保存按钮点击时回调,返回富文本内容
这就是小程序中的富文本编辑器:

接下来我们说下,当在富文本编辑器中录入好信息并提交到云数据库中,如何在详情页中查询并显示数据,这时候我们就要用到官方给提供的rich-text组件了。大家可以去官方文档中去看下该组件的详细属性(传送门)。
在详情页获取数据之前,我们先看下在富文本编辑器提交给云数据库的数据格式,

我们要的是html格式的,用rich-text组件来解析。在看看详情页中该如何查询数据并渲染到页面
<view class="warp">
<view class="details">
<view class="detailsContent">
<rich-text nodes="{
{html}}" ></rich-text>
</view>
</view>
</view>const db = wx.cloud.database();//调用默认云环境的数据库引用
const app = getApp();
Page({
data: {
details:{}
},
onLoad: function(options) {
let that=this;
db.collection("details").doc(options.id).get().then(res=>{//首先获取数据集合,查询数据,
console.log("详情数据:",res.data)
this.setData({
html: res.data[0].html.replace('<img ', '<img style="max-width:100%;height:auto;"'),
})
})
},
})大家可能看到在后面中追加了.replace('<img ', '<img style="max-width:100%;height:auto;"'),这个是针对在富文本中添加的图片,默认会很大。这样设置的话,图片就可以适配了。
这就是在详情页中的效果:

边栏推荐
- About the current response, the method getoutputstream() has been called
- Harmonyos third training notes
- 机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)
- MD5 password encryption
- STL容器——set集合的应用
- Three high concurrency methods to realize I++
- IDL调用6S大气校正
- Convolutional neural network
- 关于New_Online_Judge_1081_哥德巴赫猜想的思考
- The code of yolov5 model for pest identification in Title A of the 10th Teddy cup data mining challenge (has been run through, original works, continuously updated)
猜你喜欢
随机推荐
用户的管理-限制
Logic of image uploading
645. 错误的集合
Attendance check-in and leave system based on SSM framework
02 Bar _ Recommandation de film (basée sur le contenu) Portrait de l'utilisateur
一个问题的探讨
3. Restclient query document
CVE-2021-44228 Log4j 复现及原理
【2022第十届‘泰迪杯’挑战赛】A题:害虫识别完整版(大致思路。详细过程和代码以及结果csv在压缩包中)
用户-注册/登录
学习C语言第三天
HarmonyOS第四次培训笔记
【C语言—零基础第九课】函数中的爱恨情仇
第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)
C语言练习2
Fanoutexchange switch is simple to use
CVE-2019-14234 Django JSONField SQL注入漏洞
POC——DVWA‘s XSS Reflected
【C语言_学习_考试_复习第三课】ASCII码与C语言概述
Harmonyos second training notes









