wangeditor富文本的详细使用,wangeditor的基本配置

示例:

第一步:下载 

        npm  run  wangeditor

第二步:封装子组件 editorComponents

<template>
    <div id="editor" ref="myEditor"></div>
</template>
<script>
  import WangEditor from 'wangeditor'
  import axios from 'axios'
  // 获取token,用于上传图片
  import { ACCESS_TOKEN } from '@/store/mutation-types' 

  export default {
    name: 'ComponentWangeditor',
    data () {
      return {
        edit: '',
        btnmenu:[
            'head',//标题
            'bold',//粗体
            'fontSize',//字号
            'fontName',//字体
            'italic',//斜体
            'undeline',//下划线
            'strikeThrough',//删除线
            'foreColor',//文字颜色
            'backColor',//背景颜色
            'link',//插入链接
            'list',//列表
            'justify',//对齐方式
            'quote',//引用
            'emoticon',//表情
            'image',//插入图片
            'table',//表格
            'video',//插入视频
            'code',//插入代码
            'undo', //撤销
            'redo', // 重复
        ]
      }
    },
    props: {
      value: {
        type: String,
        default: ''
      },
    },
    methods: {
        initEditor () {
            var self = this
            // 初始化
            this.editor = new WangEditor(this.$refs.myEditor)

            this.editor.customConfig = {
                // 设置编辑器层级
                zIndex:999,
                // 配置按钮菜单
                menus:this.btnmenu,
                // 粘贴时不忽略图片
                pasteIgnoreImg: false, 
                // 去掉复制过来的样式
                pasteFilterStyle:true, 
                //限制一次最多上传 9 张图片
                uploadImgMaxLength:9, 
                //图片大小限制为2M
                uploadImgMaxSize:2*1024*1024, 
                // 自定义配置颜色(字体颜色、背景色)
                colors: ['#000000','#eeece0','#1c487f','#4d80bf','#c24f4a','#8baa4a','#7b5ba1'];
                // 自定义字体
                fontNames : ["PingFangSC"],

                // 自定义处理粘贴的文本内容
                pasteTextHandle = function (content) {
                    // content 即粘贴过来的内容(html 或 纯文本),可进行自定义处理然后返回
                    // 例如 : return content + '<p>在粘贴内容后面追加一行</p>'
                    return content
                },
            };
            
            // 配置 onchange 事件
            this.editor.change = function () { // 编辑区域内容变化时
                // 注意这里的this指向
                self.$emit('input', this.txt.html())
                self.$emit('onchange', this.txt.html(), this.txt)
                // this.txt.html('.....') //设置编辑器内容
                // this.txt.clear() //清空编辑器内容
                // this.txt.append('<p>追加的内容</p>')//继续追加内容。
                // this.txt.text()  // 读取 text
                // this.txt.getJSON()  // 获取 JSON 格式的内容
            }

            //配置上传图片
            this.editor.customConfig.customUploadImg = function (files, insert) {
                var formData = new FormData()
                files.forEach(file => {
                    formData.append('file', file)
                })
                let headers = {
                    Authorization : 'Bearer ' + ACCESS_TOKEN
                }
                axios.post('/api/sysFileInfo/commonUpload', formData, {headers}).then(({ data }) => {
                    if (data.success && data.code === 200) {
                        let  url = data.data.imgUrl
                        insert(url)
                    }
                })
            }

            this.editor.create() // 生成编辑器
            this.editor.txt.text(this.value) // 初始化的时候设置编辑器的内容
            this.$emit('oninit', this.editor)


            // this.editor.$textElem.attr('contenteditable', false);// 禁用编辑功能
            // this.editor.$textElem.attr('contenteditable', true); // 开启编辑功能
      },

    },
    mounted () {
      this.initEditor()
    }
  }
</script>

<style >
  .w-e-toolbar{
    flex-wrap:wrap;
  }

</style>

第三步:父组件使用

<template>
    <editorComponents :value="content" @onchange="changeEditor" @oninit="getEditor" ></editorComponents>    
</template>
<script>
  import editorComponents from './editorComponents'
  export default {
    components:{
        editorComponents
    },
    data () {
      return {
        content:''
      }
    },
    mounted () {
        changeEditor(e){
            console.log(e)
        },

        getEditor(e){
            console.log(e)
        }
    }
  }
</script>

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是WangEditor富文本编辑器的所有配置: 1. 基础配置 ``` editor.config.uploadImgUrl = '/upload'; // 上传图片的接口地址 editor.config.uploadImgFileName = 'file'; // 上传图片的文件名称 editor.config.menus = [ 'head', // 标题 'bold', // 粗体 'fontSize', // 字号 'fontName', // 字体 'italic', // 斜体 'underline', // 下划线 'strikeThrough', // 删除线 'foreColor', // 文字颜色 'backColor', // 背景颜色 'link', // 插入链接 'list', // 列表 'justify', // 对齐方式 'quote', // 引用 'emoticon', // 表情 'image', // 插入图片 'table', // 表格 'video', // 插入视频 'code', // 插入代码 'undo', // 撤销 'redo' // 重复 ]; ``` 2. 自定义菜单 ``` editor.config.menus = [ 'head', // 标题 'bold', // 粗体 'fontSize', // 字号 'fontName', // 字体 'italic', // 斜体 'underline', // 下划线 'strikeThrough', // 删除线 'foreColor', // 文字颜色 'backColor', // 背景颜色 'link', // 插入链接 'list', // 列表 'justify', // 对齐方式 'quote', // 引用 'emoticon', // 表情 'image', // 插入图片 'table', // 表格 'video', // 插入视频 'code', // 插入代码 'undo', // 撤销 'redo', // 重复 { name: 'customMenu1', // 菜单名称 text: '自定义菜单1', // 显示的文本 title: '自定义菜单1', // 鼠标悬浮时显示的文本 exec: function () { // 点击菜单时触发的回调函数 // 自定义菜单1的代码 } }, { name: 'customMenu2', text: '自定义菜单2', title: '自定义菜单2', exec: function () { // 自定义菜单2的代码 } } ]; ``` 3. 图片上传配置 ``` editor.config.uploadImgUrl = '/upload'; // 上传图片的接口地址 editor.config.uploadImgFileName = 'file'; // 上传图片的文件名称 editor.config.uploadImgFns.onload = function (resultText, xhr) { // 图片上传成功时的回调函数 // 图片上传成功的处理代码 }; editor.config.uploadImgFns.onerror = function (xhr) { // 图片上传失败时的回调函数 // 图片上传失败的处理代码 }; editor.config.uploadImgParams = { // 上传图片时需要携带的参数 'token': '123456' }; ``` 4. 视频上传配置 ``` editor.config.uploadVideoUrl = '/upload'; // 上传视频的接口地址 editor.config.uploadVideoFileName = 'file'; // 上传视频的文件名称 editor.config.uploadVideoFns.onload = function (resultText, xhr) { // 视频上传成功时的回调函数 // 视频上传成功的处理代码 }; editor.config.uploadVideoFns.onerror = function (xhr) { // 视频上传失败时的回调函数 // 视频上传失败的处理代码 }; editor.config.uploadVideoParams = { // 上传视频时需要携带的参数 'token': '123456' }; ``` 5. 表格配置 ``` editor.config.menus = [ 'head', // 标题 'bold', // 粗体 'fontSize', // 字号 'fontName', // 字体 'italic', // 斜体 'underline', // 下划线 'strikeThrough', // 删除线 'foreColor', // 文字颜色 'backColor', // 背景颜色 'link', // 插入链接 'list', // 列表 'justify', // 对齐方式 'quote', // 引用 'emoticon', // 表情 'image', // 插入图片 'table', // 表格 'video', // 插入视频 'code', // 插入代码 'undo', // 撤销 'redo' // 重复 ]; editor.config.menusConfig = { // 表格菜单的配置 table: { dropdown: ['insert', 'delete', 'colBefore', 'colAfter', 'rowBefore', 'rowAfter', 'colMerge', 'rowMerge', 'format'], trigger: '<span class="wangeditor-menu-img-table"></span>', className: '' } }; editor.config.tableDropdownConfig = { // 表格下拉菜单的配置 insert: { text: '插入表格', handler: function () { // 插入表格的代码 } }, delete: { text: '删除表格', handler: function () { // 删除表格的代码 } }, colBefore: { text: '在左侧插入列', handler: function () { // 在左侧插入列的代码 } }, colAfter: { text: '在右侧插入列', handler: function () { // 在右侧插入列的代码 } }, rowBefore: { text: '在上方插入行', handler: function () { // 在上方插入行的代码 } }, rowAfter: { text: '在下方插入行', handler: function () { // 在下方插入行的代码 } }, colMerge: { text: '合并列', handler: function () { // 合并列的代码 } }, rowMerge: { text: '合并行', handler: function () { // 合并行的代码 } }, format: { text: '表格格式化', handler: function () { // 表格格式化的代码 } } }; editor.config.tableDefaultStyle = { // 表格默认样式 borderWidth: '1px', borderColor: '#ccc', width: '100%' }; editor.config.tableInsertMaxRow = 10; // 插入表格时最大行数 editor.config.tableInsertMaxCol = 10; // 插入表格时最大列数 ``` 希望这些信息对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值