【uniapp】【微信小程序】富文本编辑器Editor之setContents初始化富文本内容失败(报错)

在uniapp的微信小程序项目中,使用Editor组件时遇到setContents初始化富文本内容失败的问题。原因是setContents方法在mounted阶段可能未准备好。解决方案是在setContents的成功回调中设置定时器,通过监听isOk状态来确定是否成功,并在成功时清除定时器,以提高用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目场景:

进入页面初始化时,需要填入之前预填写的富文本内容,使用uniapp中editor组件setContents方法为富文本编辑器设置内容时,内容有时不时设置不成功,微信小程序同样适用


问题描述

使用uniapp中editor组件setContents方法为富文本编辑器设置内容时,在mounted时调用setContents方法,富文本内容有时设置不成功

样例代码:

<template>
	<editor id="editor"></editor>
</template>
<script>
export default {
	data() {
		html: '<p>富文本测试</p>',
		editorCtx : ''
	},
	mounted() {
		let _this = this
		// #ifdef APP-PLUS || H5 ||MP-WEIXIN
		uni.createSelectorQuery().select('#editor').context((res) => {
			_this.editorCtx = res.context //uni.createSelectorQuery获取editor 组件对应的 editorContext实例
		}).exec()
		_this.editorCtx.setContents({
			'html': _this.html,
			success:  (res)=> {
				console.log(res,"res")
			},
			fail:(err)=> {
				console.log(err,"err")
			}
		})
		// #endif
	}
}

原因分析:

在mounted时调用setContents方法会返回两个回调,一个成功回调,一个失败回调

通过在setContents中打印回调函数结果,分析得知是setContents调用失败即未准备好

  1. 成功时函数回调图片如下
    setContents函数成功时回调
  2. 报错时函数回调图片如下
    setContents函数报错时回调

解决方案:

通过在setContents成功的回调时设置定时器(简单粗暴),通过监听isOk值判断setContents函数是否成功,成功时清除定时器。

解决代码如下:

<template>
	<editor id="editor"></editor>
</template>
<script>
export default {
	data() {
		html: '<p>富文本测试</p>',
		editorCtx : '',
		isOk: false
	},
	watch: {
		isOk() {	//监听isOk属性,为true时说明setContents已执行成功的回调,此时清除timer定时器
			this.isOk && clearInterval(this.timer)
		}
	},
	mounted() {
		let _this = this
		// #ifdef APP-PLUS || H5 ||MP-WEIXIN
		uni.createSelectorQuery().select('#editor').context((res) => {
			_this.editorCtx = res.context	//uni.createSelectorQuery获取editor 组件对应的 editorContext实例
		}).exec()
		_this.timer =  setInterval(() => { //设置timer定时器触发setContents函数
			_this.editorCtx.setContents({
				'html': _this.html,
				success:  (res)=> {
					console.log(res,"res")
					_this.isOk = true //成功的回调,说明富文本已设置内容,此时将isOk属性设为true
				},
				fail:(err)=> {
					console.log(err,"err")
				}
			})
		})
		// #endif
	}
}

!!修改版!!

防止app端卡死,新增500毫秒延迟和loading效果,增加用户体验

解决代码如下:

<template>
	<editor id="editor"></editor>
</template>
<script>
export default {
	data() {
		html: '<p>富文本测试</p>',
		editorCtx : '',
		isOk: false
	},
	watch: {
		isOk() {	//监听isOk属性,为true时说明setContents已执行成功的回调,此时清除timer定时器
			this.isOk && clearInterval(this.timer) && uni.hideLoading()
		}
	},
	mounted() {
		let _this = this
		// #ifdef APP-PLUS || H5 ||MP-WEIXIN
		uni.createSelectorQuery().select('#editor').context((res) => {
			_this.editorCtx = res.context	//uni.createSelectorQuery获取editor 组件对应的 editorContext实例
		}).exec()
		uni.showloading({ //增加loading效果,优化用户体验
			title: "加载中"
		})
		_this.timer =  setInterval(() => { //设置timer定时器触发setContents函数
			_this.editorCtx.setContents({
				'html': _this.html,
				success:  (res)=> {
					console.log(res,"res")
					_this.isOk = true //成功的回调,说明富文本已设置内容,此时将isOk属性设为true
				},
				fail:(err)=> {
					console.log(err,"err")
				}
			})
		}, 500) //添加一个500毫秒的延迟,减少app端压力(兼容app端)
		// #endif
	}
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值