Vue项目中-富文本编辑器的简单使用

1、安装依赖

cnpm i wangeditor --save

2、组件中的基本使用

1)准备 dom

<div id="editor"></div>

2)在组件中导入

import Editor from "wangeditor"

2)在 mounted() 中使用

// 这里在 data() 中声明了一个 editor
data () {
	return {
		editor: null,
		serverIp: 'http://localhost:3000',
		goods: {
			mainPic: []
		}
	}
}
mounted: {
	// 首次操作dom 在这里
	// 富文本编译
	// 拿到dom 元素
	this.editor = new Editor("#editor");
	
	// 添加配置项,必须在 create() 方法之前配置
	// 0-设置 zindex
	this.editor.config.zIndex = 0;
	// 1-设置编辑区域高度为 200px
	this.editor.config.height = 200;
	// 2-默认情况下,显示所有菜单
	this.editor.config.menus = [
      "head",
      "bold",
      "fontSize",
      "fontName",
      "italic",
      "underline",
      // 'strikeThrough',
      // 'indent',
      // 'lineHeight',
      "foreColor",
      // 'backColor',
      // 'link',
      // 'list',
      // 'todo',
      // 'justify',
      // 'quote',
      // 'emoticon',
      "image",
      // 'video',
      // 'table',
      // 'code',
      // 'splitLine',
      // 'undo',
      // 'redo',
    ];
    // 3-配置上传图片
    // 3-1 配置上传服务器接口
    this.editor.config.uploadImgServer = this.serverIp + "/upload/img";
    // 3-2 配置上传字段名
    this.editor.config.uploadFileName = "mainPic";
    // 3-3 图片上传的钩子函数
    this.editor.config.uploadImgHooks = {
      // 图片上传并返回了结果,想要自己把图片插入到编辑器中
      customInsert: (insertImgFn, result) => {
        // 用箭头函数解决 this 指向问题
        // result 即服务端返回的接口
        // console.log("customInsert", result);

        // insertImgFn 可把图片插入到编辑器,传入图片 src ,执行函数即可
        insertImgFn(`${this.serverIp}${result.file}`);
      },
    };

    this.editor.create(); // 调用 create() 方法
}

使用的富文本编辑器,官网wangEditor

———————————————————
“红绳牵不动核桃,簪子上雕刻一朵牡丹”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值