vue+wangEditor的富文本编辑器的使用

news/2024/7/10 1:14:06 标签: vue

vuewangEditor_0">vue+wangEditor的富文本编辑器的使用

先配置新建一个

vue"><el-form-item label="源码简介" prop="desc">
    <div id="editor"></div>//编辑器的位置
</el-form-item>

data(){
	return{
	  //编辑器
      editor: null,
      editorData: '',
    }
}
methods: {
	getEditorData() {
      // 通过代码获取编辑器内容
      let data = this.editor.txt.html()
    },
	beforeDestroy() {
        // 调用销毁 API 对当前编辑器实例进行销毁
        this.editor.destroy()
        this.editor = null
  },
}
mounted(){
	 const editor = new wangEditor(`#editor`)
    // 配置 onchange 回调函数,将数据同步到 vue 中
    editor.config.onchange = (newHtml) => {
       this.editorData = newHtml
    }
    editor.config.showLinkImg = false //隐藏网络图片
    editor.config.uploadImgServer = `${this.$baseUrl}/image/upload`  //图片上传的地址
    editor.config.uploadFileName = 'file' //文件传给后端的名称。类似formData.append("file", param.file);中的file
    editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp']//上传图片的类型
    editor.customconfig.uploadimgheaders = {// 上传的请求头部
       'accept': '*/*',
       'authorization':'bearer ' 
    }
    editor.config.uploadImgParams = { //上传请求的参数
      user_id: localStorage.user_id,
      s_id: localStorage.s_id,
      random:this.random,
      img_kind:'open'
    }
    editor.config.uploadImgHooks = { //上传图片的可使用回调函数
        // 上传图片之前
        before: function(xhr) {
            console.log(xhr)

            // 可阻止图片上传
            // return {
            //     prevent: true,
            //     msg: '需要提示给用户的错误信息'
            // }
        },
        // 图片上传并返回了结果,图片插入已成功
        success: function(xhr) {
            console.log('success', xhr)
        },
        // 图片上传并返回了结果,但图片插入时出错了
        fail: function(xhr, editor, resData) {
            console.log('fail', resData)
        },
        // 上传图片出错,一般为 http 请求的错误
        error: function(xhr, editor, resData) {
            console.log('error', xhr, resData)
        },
        // 上传图片超时
        timeout: function(xhr) {
            console.log('timeout')
        },
        // 图片上传并返回了结果,想要自己把图片插入到编辑器中
        // 例如服务器端返回的不是 { errno: 0, data: [...] } 这种格式,可使用 customInsert
        customInsert: function(insertImgFn, result) {
            // result 即服务端返回的接口
            console.log('customInsert', result)

            // insertImgFn 可把图片插入到编辑器,传入图片 src ,执行函数即可
            insertImgFn(result.url)
        }
    }
     // 创建编辑器
    editor.create()
    this.editor = editor
}

文本编辑器的回显

this.editor.txt.html(`${res.src_desc}`)//编辑器回显

清空内容

this.editor.txt.html('<p><br></p>')

获取编辑器的内容

// 获取编辑器区域完整html代码
this.editor.txt.html();

// 获取编辑器纯文本内容
this.editor.txt.text();

// 获取格式化后的纯文本
this.editor.txt.formatText();

//编辑器追加新内容
this.editor.txt.append('<p>新追加的内容</p>');

编辑器的启用与禁用

// 禁用
    editor.disable();
// 启用
    editor.enable();

编辑器的z-index问题,有时候编辑器会因为权重的问题覆盖了其他的组件的使用

// 将全屏时z-index修改为20000,要写在编辑器创建之前

    editor.config.zindex = 20000;
    
    editor.create();
    

或者也可以通过css来修改

/* 富文本编辑器 */
.w-e-toolbar {
  z-index: 2 !important;
}
.w-e-text-container {
  z-index: 1 !important;
}

http://www.niftyadmin.cn/n/861530.html

相关文章

vue限制不登录无法进入其他页面

vue限制不登录&#xff0c;通过url进入其他页面强制回到登录页面;已经登录了&#xff0c;不可以再进入登录界面 先在router下的index.js添加 meta:{requireAuth:true}&#xff0c;如下 {path: /data,name: data,component: data,meta:{requireAuth:true} },然后在main.js添加…

vacode常用快捷键大全

vscode的快捷键 1.快速复制上下行 shift alt ↑2.快速选中相同的单词&#xff0c;然后批量修改 Ctrl d3.快速查找相同的单词 Ctrl f4.关闭侧边栏 Ctrl b5.跳到特定的行 Ctrl g6.选择特定标签或者变量等等跳转 shift Ctrl o*通用*CtrlShiftP, F1打开命令面板Ctrl…

axios的请求拦截器和动态loading的

axios的请求拦截器和动态加载loading&#xff0c;两个结合 在这里使用的是&#xff0c;vueaxioselementUI来实现的 import axios from axios // axios.defaults.baseURL /api import browser from ../common/browser import qs from qs;import { Loading } from element-ui …

计网笔记(一)

1.物理层 物理层更多的是规定一种标准&#xff0c;他并不管物理介质具体是什么&#xff0c;比如电线杆上是光纤还是双绞线&#xff0c;只要你能按物理层规定的标准传输数据就行。 物理层是规定传输媒体接口的标准 2.数据链路层 数据链路层的主要功能 (1) 封装成帧 数据链路…

vscode的针对vue的template的注释失效变成//

vscode的针对vue中template的注释失效变成// 忘记那一天了&#xff0c;vscode提示安装一个插件&#xff0c;Vue 3 Snippets Highlight Formatters And Generator&#xff0c;我就顺手点了一下&#xff1b;然后就发现我的快捷键注释template的变成了下面这个样式。所以只要把这…

box-shadow使用

box-shadow&#xff1a; 参数 x:水平阴影&#xff0c;可以为负值&#xff0c;当为负值时&#xff0c;阴影从外面&#xff08;默认为外阴影&#xff09;向左移动&#xff1b;当为正值时&#xff0c;阴影从外面&#xff08;默认为外阴影&#xff09;向右移动。即正方向为向右和向…

vue创建WebSocket进行实时通讯

vueWebSocket创建实现实时通讯 //websocketasync initWebSocket() {if (websock) {return;}if (!const_chatid) return;if (typeof WebSocket "undefined") {console.error("您的浏览器不支持WebSocket");return false;}try {let params {user_id: loca…

elementUI的动态tabs页的使用,vue的动态组件的操作

elementUI的动态tabs页的使用&#xff0c;vue的动态组件的操作 有时候我们需要用到动态的tab页&#xff0c;结合不同的页面内容来显示。这里是使用了elementUI的动态tabs页来实现的 <div class"right" v-loading"loading"><div class"betw…