uni-app 富文本组件editor无法正常使用
uni-app 富文本组件editor无法正常使用
| 项目信息 | 详细信息 |
|---|---|
| 产品分类 | uniapp/H5 |
| PC开发环境操作系统 | Windows |
| PC开发环境操作系统版本号 | 10 |
| HBuilderX类型 | 正式 |
| HBuilderX版本号 | 3.99 |
| 浏览器平台 | Chrome |
| 浏览器版本 | 120.0.6099.217 |
| 项目创建方式 | HBuilderX |
示例代码:
<template>
<view class="jnpf-editor">
<template v-if="!detailed">
<view class='toolbar'>
<view :class="{'ql-active':formats.bold}" class="iconfont icon-zitijiacu" data-name="bold"
@tap="format"></view>
<view :class="{'ql-active':formats.italic}" class="iconfont icon-zitixieti" data-name="italic"
@tap="format"></view>
<view :class="{'ql-active':formats.underline}" class="iconfont icon-zitixiahuaxian"
data-name="underline" @tap="format"></view>
<view :class="{'ql-active':formats.strike}" class="iconfont icon-zitishanchuxian" data-name="strike"
@tap="format"></view>
<view :class="{'ql-active':formats.align==='left'}" class="iconfont icon-zuoduiqi" data-name="align"
data-value="left" @tap="format"></view>
<view :class="{'ql-active':formats.align==='center'}" class="iconfont icon-juzhongduiqi"
data-name="align" data-value="center" @tap="format"></view>
<view :class="{'ql-active':formats.align==='right'}" class="iconfont icon-youduiqi" data-name="align"
data-value="right" @tap="format"></view>
<view :class="{'ql-active':formats.align==='justify'}" class="iconfont icon-zuoyouduiqi"
data-name="align" data-value="justify" @tap="format"></view>
<view :class="{'ql-active':formats.lineHeight}" class="iconfont icon-line-height" data-name="lineHeight"
data-value="2" @tap="format"></view>
<view :class="{'ql-active':formats.letterSpacing}" class="iconfont icon-Character-Spacing"
data-name="letterSpacing" data-value="2em" @tap="format"></view>
<view :class="{'ql-active':formats.marginTop}" class="iconfont icon-722bianjiqi_duanqianju"
data-name="marginTop" data-value="20px" @tap="format"></view>
<view :class="{'ql-active':formats.marginBottom}" class="iconfont icon-723bianjiqi_duanhouju"
data-name="marginBottom" data-value="20px" @tap="format"></view>
<view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
<view :class="{'ql-active':formats.fontFamily}" class="iconfont icon-font" data-name="fontFamily"
data-value="Pacifico" @tap="format"></view>
<view :class="{'ql-active':formats.fontSize === '24px'}" class="iconfont icon-fontsize"
data-name="fontSize" data-value="24px" @tap="format"></view>
<view :class="{'ql-active':formats.color === '#0000ff'}" class="iconfont icon-text_color"
data-name="color" data-value="#0000ff" @tap="format"></view>
<view :class="{'ql-active':formats.backgroundColor === '#00ff00'}" class="iconfont icon-fontbgcolor"
data-name="backgroundColor" data-value="#00ff00" @tap="format"></view>
<view class="iconfont icon-date" @tap="insertDate"></view>
<view class="iconfont icon--checklist" data-name="list" data-value="check" @tap="format"></view>
<view :class="{'ql-active':formats.list === 'ordered'}" class="iconfont icon-youxupailie"
data-name="list" data-value="ordered" @tap="format"></view>
<view :class="{'ql-active':formats.list === 'bullet'}" class="iconfont icon-wuxupailie" data-name="list"
data-value="bullet" @tap="format"></view>
<view class="iconfont icon-undo" @tap="undo"></view>
<view class="iconfont icon-redo" @tap="redo"></view>
<view class="iconfont icon-outdent" data-name="indent" data-value="-1" @tap="format"></view>
<view class="iconfont icon-indent" data-name="indent" data-value="+1" @tap="format"></view>
<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
<view class="iconfont icon-charutupian" @tap="insertImage"></view>
<view :class="{'ql-active':formats.header === 1}" class="iconfont icon-format-header-1"
data-name="header" :data-value="1" @tap="format"></view>
<view :class="{'ql-active':formats.script === 'sub'}" class="iconfont icon-zitixiabiao"
data-name="script" data-value="sub" @tap="format"></view>
<view :class="{'ql-active':formats.script === 'super'}" class="iconfont icon-zitishangbiao"
data-name="script" data-value="super" @tap="format"></view>
<view class="iconfont icon-shanchu" @tap="clear"></view>
<view :class="{'ql-active':formats.direction === 'rtl'}" class="iconfont icon-direction-rtl"
data-name="direction" data-value="rtl" @tap="format"></view>
</view>
<view class="editor-wrapper">
<editor :id="id" class="ql-container" :placeholder="placeholder" showImgSize showImgToolbar
showImgResize @statuschange="onStatusChange" :read-only="disabled" [@ready](/user/ready)="onEditorReady"
@input="getValue"></editor>
</view>
</view>
<view v-else>
<mp-html class="editor-box" :content="modelValue" />
</view>
</template>
</view>
<script>
export default {
name: 'jnpf-editor',
props: {
modelValue: {
type: String,
default: ''
},
placeholder: {
type: String,
default: '请输入'
},
disabled: {
type: Boolean,
default: false
},
detailed: {
type: Boolean,
default: false
},
},
data() {
return {
innerValue: '',
readOnly: false,
formats: {},
editorChange: false,
id: 'jnpf-' + this.jnpf.idGenerator(),
}
},
watch: {
modelValue(val) {
if (this.editorChange && val) return
this.editorChange = false
this.editorCtx && this.editorCtx.setContents({
html: val
})
}
},
onLoad() {
uni.loadFontFace({
family: 'Pacifico',
source: 'url("https://sungd.github.io/Pacifico.ttf")'
})
},
methods: {
readOnlyChange() {
this.readOnly = !this.readOnly
},
onEditorReady() {
// #ifdef APP-PLUS || H5 ||MP-WEIXIN
uni.createSelectorQuery().in(this).select('#' + this.id).context((res) => {
this.editorCtx = res.context
this.editorCtx.setContents({
html: this.modelValue
})
}).exec()
// #endif
},
undo() {
this.editorCtx.undo()
},
redo() {
this.editorCtx.redo()
},
format(e) {
let {
name,
value
} = e.target.dataset
if (!name) return
this.editorCtx.format(name, value)
},
onStatusChange(e) {
const formats = e.detail
this.formats = formats
},
insertDivider() {
this.editorCtx.insertDivider()
},
clear() {
this.editorCtx.clear()
},
removeFormat() {
this.editorCtx.removeFormat()
},
insertDate() {
const date = new Date()
const formatDate = ${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}
this.editorCtx.insertText({
text: formatDate
})
},
insertImage() {
uni.chooseImage({
count: 1,
success: (res) => {
this.getImageBase64(res)
}
})
},
getImageBase64(res) {
const image = res.tempFilePaths[0]
// #ifdef MP-WEIXIN
uni.getFileSystemManager().readFile({
filePath: image,
encoding: "base64",
success: (e) => {
this.insertImageVal('data:image/jpeg;base64,' + e.data)
},
});
// #endif
// #ifdef APP-PLUS
let path = plus.io.convertLocalFileSystemURL(image);
let fileReader = new plus.io.FileReader();
fileReader.readAsDataURL(path);
fileReader.onloadend = (e) => {
this.insertImageVal(e.target.result);
}
// #endif
// #ifdef H5
uni.request({
url: image, //临时路径
responseType: 'arraybuffer', //设置返回的数据格式为arraybuffer
success: res => {
const base64 = wx.arrayBufferToBase64(res.data)
this.insertImageVal('data:image/jpeg;base64,' + base64);
},
})
// #endif
},
insertImageVal(image) {
this.editorCtx.insertImage({
src: image,
alt: '图像',
success: function() {}
})
},
getValue(e) {
this.editorChange = true
let val = e.detail.text === '\n' ? '' : e.detail.html
this.$emit('update:modelValue', val)
}
}
}
</script>
<style lang="scss" scoped>
@import "./editor-icon.css";
:deep(.ql-editor) {
word-break: break-all;
}
.jnpf-editor {
background-color: #fff;
.iconfont {
display: inline-block;
width: 80rpx;
height: 80rpx;
cursor: pointer;
font-size: 20px;
line-height: 80rpx;
text-align: center;
}
.toolbar {
height: 240rpx;
background: #f5f5f5;
overflow-y: auto;
box-sizing: border-box;
border-bottom: 0;
font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
}
.ql-container {
box-sizing: border-box;
padding: 20rpx;
width: 100%;
height: 400rpx;
margin-top: 20rpx;
font-size: 30rpx;
line-height: 1.5;
}
.ql-active {
color: #06c;
}
}
</style>
操作步骤:
无法复现
预期结果:
正常使用就行
实际结果:
@ready 初始化方法都没有进入无法使用
bug描述:
editor组件无法使用
更多关于uni-app 富文本组件editor无法正常使用的实战教程也可以访问 https://www.itying.com/category-93-b0.html
回复 s***@yinmaisoft.com: https://vue3-hellouniapp.dcloud.net.cn/pages/component/editor/editor
你好!我发现在Microsoft Edge浏览器上也不行 版本 120.0.2210.133 (正式版本) (64 位)
这段示例代码不方便我们定位你的问题,请提供一个简单复现的demo(zip压缩包附件上传
官网预览都不行https://hellouniapp.dcloud.net.cn/pages/component/editor/editor
之前我 vue2 搞定了,不知道怎么又回去了,而且左上角跳转 vue3 的内容也不见了,你瞧瞧
回复 DCloud_UNI_OttoJi: 我在一月十号的时候上传了一版,我下午核对一下版本差异
现在可以了谢谢
是按照文档和 hello uniapp 修改的吗,欢迎反馈
你好!我发现在Microsoft Edge浏览器上不行 版本 120.0.2210.133 (正式版本) (64 位)
用不了,看下是因为报错的原因,还是因为 network 访问了 unpkg 的原因?如果是后者可以参考 https://ask.dcloud.net.cn/article/40900 解决
你在之前显示正常后,你是做了什么修改才导致这个问题复现,方便贴一下代码吗
回复 HRK_01: 我都没有做如何修改
回复 s***@yinmaisoft.com: 目前新版本已发布到github,通过github克隆hello-uniapp项目并运行即可解决该问题,稍后会将最新版本同步到HBX新建的hello示例工程。
回复 HRK_01: 现在可以使用了,那请问这个富文本如何添加表格
回复 s***@yinmaisoft.com: 目前官方自带的富文本暂不支持添加表格,如果有需求,可到插件市场下载相关富文本插件使用 https://ext.dcloud.net.cn/search?q=富文本&cat1=3
我发现在vue3的时候 picker-view 这个控件的value传不进去 :value=“valueArr” 导致时间没办法定位到当前年月日 定位到了起始时间 就算这样写死也不生效 :value="[2,3,6]" 同样的数据vue2就可以
感谢反馈,我将尝试验证并在此更新进度
已复现该问题,感谢你的反馈,后续尝试修复,并在此贴更新
回复 HRK_01: 你好这个picker-view修复好了吗?
回复 s***@yinmaisoft.com: 你好,十分抱歉,之前的排查方式有误,现重新排查了一下,底层逻辑并无问题,发现并不是value不生效,而是传值的时候应该传数组的下标,而不是值。如果你传入下标还是不生效的话,能否提供一个简单的demo(zip压缩包)
回复 HRK_01: 我截图时传的就是数组下标,相同控件,相同数据,相同写法,在vue3就传不过去
回复 s***@yinmaisoft.com: 是app端,H5可以
还有两个页面反复打开来回切换会导致页面卡死,直到浏览器崩溃,app也是相同问题
回复 s***@yinmaisoft.com: 能否提供一下问题工程
回复 HRK_01: picker-view这个控件在vue3 app端 :value 值传不进去 写死数组下标也不行
回复 s***@yinmaisoft.com: 我在vue3中并未复现该问题哦,编译到IOS与安卓都能正常使用,能贴一下代码吗
// 小于10前面补0,用于月份,日期,时分秒等 formatNumber(num) { return +num < 10 ? ‘0’ + num : String(num); }, // 获取时间戳 getTimestamp() { let format = this.jnpf.handelFormat(this.format) let timeType = format === ‘yyyy’ ? ‘/01/01 00:00:00’ : format === ‘yyyy-MM’ ? ‘/01 00:00:00’ : format === ‘yyyy-MM-dd’ ? ’ 00:00:00’ : ‘’ // yyyy-mm-dd为安卓写法,不支持iOS,需要使用"/"分隔,才能二者兼容 let time = “”; if (this.params.year && !this.params.month && !this.params.day && !this.params.hour && !this.params .minute && !this.params.second) { time = this.year + timeType } else if (this.params.year && this.params.month && !this.params.day && !this.params.hour && !this.params .minute && !this.params.second) { time = this.year + ‘/’ + this.month + timeType } else if (this.params.year && this.params.month && this.params.day && !this.params.hour && !this.params .minute && !this.params.second) { time = this.year + ‘/’ + this.month + ‘/’ + this.day + timeType } else if (this.params.year && this.params.month && this.params.day && this.params.hour && !this.params .minute && !this.params.second) { time = this.year + ‘/’ + this.month + ‘/’ + this.day + " " + this.hour + timeType } else if (this.params.year && this.params.month && this.params.day && this.params.hour && this.params .minute && !this.params.second) { time = this.year + ‘/’ + this.month + ‘/’ + this.day + " " + this.hour + “:” + this.minute + timeType } else { time = this.year + ‘/’ + this.month + ‘/’ + this.day + " " + this.hour + “:” + this.minute + “:” + this .second + timeType } return new Date(time).getTime(); }
在使用 uni-app 的富文本组件 editor 时,如果遇到无法正常使用的情况,可能是由于多种原因导致的。以下是一些常见的问题及其解决方法:
1. 平台兼容性问题
editor组件在部分平台(如 H5、小程序)上可能存在兼容性问题。确保你使用的平台支持editor组件。- 在
H5端,editor组件可能无法正常使用,建议使用web-view组件嵌入第三方富文本编辑器。
2. 版本问题
- 确保你使用的
uni-app版本是最新的,旧版本可能存在一些已知的 bug。 - 更新
uni-app到最新版本,或者检查是否有相关的 bug 修复。
3. 组件配置问题
- 检查
editor组件的配置是否正确。例如,placeholder、read-only等属性是否设置正确。 - 确保
editor组件的id是唯一的,避免与其他组件冲突。
4. 样式问题
editor组件的样式可能会受到父组件样式的影响,导致显示异常。检查是否有样式冲突,并尝试重置editor组件的样式。
5. 事件绑定问题
- 确保你正确绑定了
editor组件的事件,如input、focus、blur等。事件处理函数中可能存在逻辑错误,导致组件无法正常工作。
6. 数据绑定问题
- 如果使用
v-model进行数据绑定,确保绑定的数据是响应式的,并且在数据变化时能够正确更新editor组件的内容。
7. 调试工具
- 使用
uni-app提供的调试工具(如HBuilderX的调试功能)来检查editor组件的运行状态,查看是否有错误信息输出。
8. 替代方案
- 如果
editor组件在特定平台上无法正常使用,可以考虑使用第三方富文本编辑器(如wangEditor、quill等),并通过web-view组件嵌入到uni-app中。
示例代码
以下是一个简单的 editor 组件使用示例:
<template>
<view>
<editor
id="myEditor"
placeholder="请输入内容"
@input="onInput"
@focus="onFocus"
@blur="onBlur"
></editor>
</view>
</template>
<script>
export default {
methods: {
onInput(e) {
console.log('输入内容:', e.detail.html);
},
onFocus() {
console.log('编辑器获得焦点');
},
onBlur() {
console.log('编辑器失去焦点');
}
}
};
</script>
<style>
/* 自定义样式 */
</style>


