前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue 富文本编辑框_基于vue的富文本编辑器

vue 富文本编辑框_基于vue的富文本编辑器

作者头像
全栈程序员站长
发布2022-11-08 11:43:12
1.1K0
发布2022-11-08 11:43:12
举报
文章被收录于专栏:全栈程序员必看

1、下载插件

npm i wangeditor –save 插件官网地址:https://www.wangeditor.com/

2、封装富文本组件

代码语言:javascript
复制
<template lang="html">
<div class="editor">
<!-- <div ref="toolbar" class="toolbar"></div> -->
<div ref="editor" class="text"></div>
</div>
</template>
<script>
import E from 'wangeditor'
export default {
name: 'editoritem',
data() {
return {
// uploadPath,
editor: null,
info_: null
}
},
model: {
prop: 'value',
event: 'change'
},
props: {
value: {
type: String,
default: ''
},
isClear: {
type: Boolean,
default: false
}
},
watch: {
isClear(val) {
// 触发清除文本域内容
if (val) {
this.editor.txt.clear()
this.info_ = null
}
},
value: function(value) {
if (value !== this.editor.txt.html()) {
this.editor.txt.html(this.value)
}
}
//value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
},
mounted() {
this.seteditor()
},
beforeDestroy() {
// 调用销毁 API 对当前编辑器实例进行销毁
this.editor.destroy()
this.editor = null
},
methods: {
seteditor() {
this.editor = new E( this.$refs.editor)
// this.editor.customConfig = this.editor.customConfig ? this.editor.customConfig : this.editor.config;
// 配置 server 上传图片接口地址
// this.editor.config.uploadImgServer = '/upload-img'
this.editor.config.onchange = (html) => {
this.info_ = html // 绑定当前值
this.$emit('change', this.info_) // 将内容同步到父组件中
}
// 自定义 alert
this.editor.config.customAlert = function (s, t) {
switch (t) {
case 'success':
this.$Message.success(s)
break
case 'info':
this.$Message.info(s)
break
case 'warning':
this.$Message.warning(s)
break
case 'error':
this.$Message.error(s)
break
default:
this.$Message.info(s)
break
}
}
// 配置全屏功能按钮是否展示
this.editor.config.showFullScreen = false
// 创建富文本编辑器
this.editor.create()
this.editor.txt.html(this.value) // 重新设置编辑器内容
}
}
}
</script>
<style lang="scss">
.editor {
width: 100%;
margin: 0 auto;
position: relative;
z-index: 0;
}
.toolbar {
border: 1px solid #ccc;
}
.text {
border: 1px solid #ccc;
min-height: 200px;
.w-e-text-container{
height: 200px!important;
}
.w-e-text{
min-height: 200px;
}
}
</style>

3、使用组件

代码语言:javascript
复制
<template>
<div>
<wangEnduit
v-model="Content"
:isClear="isClear"
@change="change"
:value="Content"
/>
</div>
</template>
<script>
export default {
components: {
wangEnduit: () => import('@/components/wangEnduit/index'),
},
data(){
return{
Content:"",
isClear: false,
}
},
methods:{
change(val) {
console.log('val',val)
},
}
}
</script>
<style>
</style>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/184117.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月9日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、下载插件
  • 2、封装富文本组件
  • 3、使用组件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档