欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > wangEdtior富文本编辑器 结合element使用,博客必备,通俗易懂

wangEdtior富文本编辑器 结合element使用,博客必备,通俗易懂

2025/2/22 16:57:19 来源:https://blog.csdn.net/Zwwxd666/article/details/140348673  浏览:    关键词:wangEdtior富文本编辑器 结合element使用,博客必备,通俗易懂

效果展示

使用步骤

安装

 npm install @wangeditor/editor-for-vue --save

创建自定义组件

image-20240711120301738

引入组件并注册,使用组件

<template><div style="border: 1px solid #ccc;"><Toolbarstyle="border-bottom: 1px solid #ccc":editor="editor":default-config="toolbarConfig":mode="mode"/><Editorv-model="html"style="height: 200px; overflow-y: hidden;":default-config="editorConfig":mode="mode"@onCreated="onCreated"@onChange="onChange"/></div>
</template>
<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'export default {components: { Editor, Toolbar },data() {return {editor: null,html: '',toolbarConfig: {},editorConfig: {autoFocus: false},mode: 'default' // or 'simple'}},created() {this.html = this.content},beforeDestroy() {const editor = this.editorif (editor == null) returneditor.destroy() // 组件销毁时,及时销毁编辑器console.log('wangEditor销毁了')},methods: {onCreated(editor) {console.log(editor)this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错},onChange(e) {console.log(e)this.$emit('EditorChange', this.html)}}
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>

需要富文本的组件里引入

image-20240711120553893

新增内容获取富文本框里的值

  1. 监听输入框

image-20240711120750866

  1. 回调触发事件,传递输入框的值 html
image-20240711120903251

image-20240711120838131

  1. 父组件监听,获取值,并设置到表单,后续请求携带该值 新增文章

image-20240711121025779

  1. 当然,我们在新增时要清空富文本框的值。设置ref然后删除

image-20240711121240337

这里用nextTick是为了等待所有dom挂载完毕,否则会出现找不到wangeditor组件问题

更新回显富文本框里的值

点击按钮,获取table组件默认插槽里传递来的所有值:scope=data也就是v-slot=scope。里面的row字段scope.row,也就是一行文章的数据,内部大概是这样

image-20240711122659066

然后还是通过ref回显设置值

image-20240711121719216

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词