uniapp中使用编辑器editor

1.需求:

开发小程序时,想要在手机上添加编辑内容,使用u--textarea时,换行操作不显示,为此使用了uniapp自带的组件editor来实现:

2.使用:

html 复制代码
<template>
	<view class="">
		<!-- <u--textarea v-model="content" placeholder="请输入活动地址..." maxlength='-1'></u--textarea> -->
		<editor id="editor" class="ql-container m-ql-container" placeholder="请输入活动地址和详细的活动介绍..."
				@ready="onEditorReady" @input="getEditorContent">
		</editor>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
			}
		},
	
		methods: {
			//1.初始化编辑器
			onEditorReady() { 
				//将content是从后台获取的字符串,将字符串转化为delta对象
				setTimeout(() => {
					var htmls = this.content
					console.log(htmls)
					if (htmls) {
						let contents = JSON.stringify(htmls)
						console.log(contents)
						uni.createSelectorQuery().select('#editor').context((res) => {
							console.log(res);
							let con = res.context
							con.setContents({
								html: htmls,
								delta: contents
							})
						}).exec()
					} else {
						return
					}
				}, 500)
			},
			//2.获取编辑器内容,当前页面都能获取到
			getEditorContent(e) { 
				this.content = e.detail.html;
			},
			
		}
	}
</script>

<style scoped>
	#editor {
		width: 100%;
		height: 300px;
		margin-top:10px;
	}
</style>

具体可参考官网:https://uniapp.dcloud.net.cn/component/editor.html

完成!

相关推荐
不务正业的前端学徒1 小时前
手写简单的call bind apply
前端
jump_jump1 小时前
Ripple:一个现代的响应式 UI 框架
前端·javascript·前端框架
用户904706683571 小时前
Nuxt css 如何写?
前端
夏天想1 小时前
element-plus的输入数字组件el-input-number 显示了 加减按钮(+ -) 和 小三角箭头(上下箭头),怎么去掉+,-或者箭头
前端·javascript·vue.js
0思必得01 小时前
[Web自动化] Selenium基础介绍
前端·python·selenium·自动化·web自动化
Filotimo_1 小时前
前端.d.ts文件作用
前端
进击的野人1 小时前
Vue 3 响应式数据解构:toRef 与 toRefs 的深度解析
前端·vue.js·前端框架
ohyeah1 小时前
CSS 作用域隔离实战:React、Vue 与 Styled Components 的三种范式
前端
二哈喇子!1 小时前
前端HTML、CSS、JS、VUE 汇总
开发语言·前端
小白路过1 小时前
node-sass和sass兼容性使用
前端·rust·sass