vue wangeditor富文本编辑器

奚泥
• 阅读 5618

wangeditor 是一款轻量级的富文本编辑器,在我的个人博客项目中用到了它,这里做一个记录。

官方地址:地址

安装:npm install wangeditor --save

在component 目录中创建 wangEditor文件夹

vue wangeditor富文本编辑器

在 wangEditor 文件夹中创建 index.js 文件

index.js中的内容

<template>
  <div ref="editor"></div>
</template>

<script>
import E from 'wangeditor'
export default {
  props: {
    value: {
      type: String,
      default: ''
    },
    meanArray: {
      // 自定义菜单
      type: Array,
      default: null
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  watch: {
    value: function (value) {
      if (value !== this.editor.txt.html()) {
        this.editor.txt.html(this.value)
      }
    }
    // value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  },
  data () {
    return {
      // 默认有这么多菜单,meanArray有值以meanArray为准
      defaultMeanus: [
        'head',
        'bold',
        'fontSize',
        'fontName',
        'italic',
        'underline',
        'strikeThrough',
        'indent',
        'lineHeight',
        'foreColor',
        'backColor',
        'link',
        'list',
        'justify',
        'quote',
        'emoticon',
        'image',
        'video',
        'table',
        'code',
        'splitLine',
        'undo',
        'redo'
      ],
      editor: ''
    }
  },
  methods: {
    init () {
      const _this = this
      this.editor = new E(this.$refs.editor)
      this.editor.config.uploadImgShowBase64 = true // 使用 base64 保存图片
      this.setMenus() // 设置菜单
      this.editor.config.onchange = (html) => {
        _this.$emit('change', html) // 将内容同步到父组件中
      }
      this.editor.create() // 创建编辑器
    },
    setMenus () {
      // 设置菜单
      if (this.meanArray) {
        this.editor.config.menus = this.meanArray
      } else {
        this.editor.config.menus = this.defaultMeanus
      }
    },
    getHtml () {
      // 得到文本内容
      return this.editor.txt.html()
    },
    setHtml (txt) {
      // 设置富文本里面的值
      this.editor.txt.html(txt)
    }
  },
  mounted () {
    const that = this
    that.$nextTick(function () {
      that.init()
    })
  }
}
</script>

在父组件中调用

<template>
 <!-- 富文本编辑器 -->
 <!-- ref 不同可以实现多个富文本编辑器 -->
 <editor ref="editorOne" v-model="detail" @change="change"></editor>
</template>

<script>
import Editor from '@/components/wangEditor'

export default {
  data () {
    return {
      // 文章内容
      detail: ''
    }
  }
  methods: {
    change (val) {
      // console.log(val)
      // console.log(this.detail)
    }
  components: { Editor }
}
</script>
点赞
收藏
评论区
推荐文章
我看sb 我看sb
3年前
基于vite+vue3+flask+mysql的玻璃态论坛
简单入门vue后的第二个实战项目,多以前端为主,python部分能用就行,以后在优化希望各位大佬能多给些建议在线预览地址:GitHub:Gitee:技术栈后端:PythonFlask数据库采用MySQL前端viteVue3yarnUI采用NaiveUI文本编辑器采用wangEditor5
Easter79 Easter79
4年前
ssm项目中ueditor富文本编辑器的使用
一、下载 https://ueditor.baidu.com/website/index.html  将ueditor放到项目中合适的位置  !(https://oscimg.oschina.net/oscnet/429e83cb838b1eb4ed6dd0d348481174948.png)二、配置文件上传路径   在utf8js
Easter79 Easter79
4年前
summernote富文本编辑器
官网:https://summernote.org/它的特点:开源使用Bootstrap3.xx到4.xx轻量级(jscss:100Kb)智能用户交互适用于所有主要浏览器:Safari,Chrome,Firefox,Opera,Edge和InternetExplorer9
编程范儿 编程范儿
4年前
项目中的富文本编辑器该如何选择?
项目中经常需要用到富文本编辑器的时候,而常见的富文本编辑器都有哪些?该如何选择?先看看市面上都有哪些可用的富文本编辑器:(插件式的,支持Vue,React,Angular框架)(Typescript开发的Web富文本编辑器,轻量、简洁、易用、开源免费,支持JS直接引入使用,或者Vue2/3,React)(开源,插件多,功能齐全,支持
Stella981 Stella981
4年前
Froala Editor破解
FroalaEditor简单破解是一款简洁漂亮,功能强大的富文本编辑器,用起来很不错,但是目前官方售价最便宜的版本三个域名一年要$239,如果不缺钱还是建议大家用正版吧,目前我是一个小破站(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.lvzaimo.com)花不起这个钱啊,
Easter79 Easter79
4年前
Spring之WebSocket
初次学习WebSocket。在本次写的WebSocketDemo里使用到了如下插件(技术):1.百度的富文本编辑器:http://ueditor.baidu.com/website/(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fueditor.baidu.com%2Fwebsit
Wesley13 Wesley13
4年前
Neditor 富文本编辑器介绍
Neditor富文本编辑器介绍Neditor是我们团队基于Ueditor的一款富文本编辑器。不论从功能还是从其它各方面来讲,Ueditor都是一款无以替代的编辑器产品。只是已经不符合现代化样式的需求,于是我们修改它的样式,实现了这样的效果:!image(https://www.notadd.com/s
Easter79 Easter79
4年前
Summernote文本编辑器入门
1、summernote是一个界面比较简洁美观的富文本编辑器。2、文件导入(官方下载地址:http://summernote.org/(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fsummernote.org%2F))下载回来的文件夹是这样的:!(https://osc
Stella981 Stella981
4年前
CKEditor5 + vue2.0 自定义图片上传、highlight、字体等用法
  因业务需求,要在vue2.0的项目里使用富文本编辑器,经过调研多个编辑器,CKEditor5 支持 vue,遂采用。因CKEditor5 文档比较少,此处记录下引用和一些基本用法。CKEditor5官网https://ckeditor.com/docs/ckeditor5/latest/builds/guides/overview.
新增富文本单元格和XSS过滤器
一.富文本单元格        皕杰设计器新增了单元格富文本类型,我们在一些网站编辑文章的时候经常可以看到富文本和markdown等编辑器,其中以Word为例,输入文字后,选择不同的功能(通常是通过点击某个图标),例如加粗或者调整字体大小,处理
GeorgeGcs GeorgeGcs
8个月前
【HarmonyOS】富文本编辑器RichEditor详解
【HarmonyOS】富文本编辑器RichEditor详解一、前言在信息化高速发展的今天,普通的文本容器,已经不能够承载用户丰富的表达欲。富文本展示已经是移动开发中,必备要解决的问题,在鸿蒙中,通过在系统层提供RichEditor控件,来解决富文本展示的问