vue2.0项目中使用Ueditor富文本编辑器示例

vue2.0项目中使用Ueditor富文本编辑器示例

最近在vue项目中需要使用富文本编辑器,于是将Ueditor集成进来,作为公共组件。
在线预览:https://suweiteng.github.io/vue2-management-platform/#/editor
项目地址:https://github.com/suweiteng/vue2-management-platform 记得在右上角点个赞哦~

1.放入静态资源并配置

首先把官网下载的Ueditor资源,放入静态资源src/static中。
vue2.0项目中使用Ueditor富文本编辑器示例
修改ueditor.config.js中的window.UEDITOR_HOME_URL配置,如下图:
vue2.0项目中使用Ueditor富文本编辑器示例

2.引入

在main.js中引入

import '../static/UE/ueditor.config.js' import '../static/UE/ueditor.all.min.js' import '../static/UE/lang/zh-cn/zh-cn.js' import '../static/UE/ueditor.parse.min.js'

3.开发公共组件

开发公共组件,可设置填充内容defaultMsg,配置信息config(宽度和高度等),并提供获取内容的方法。

<template> <div> <script id="editor" type="text/plain"></script> </div> </template> <script> export default { name: 'UE', data () { return { editor: null } }, props: { defaultMsg: { type: String }, config: { type: Object } }, mounted() { const _this = this; this.editor = UE.getEditor('editor', this.config); // 初始化UE this.editor.addListener("ready", function () { _this.editor.setContent(_this.defaultMsg); // 确保UE加载完成后,放入内容。 }); }, methods: { getUEContent() { // 获取内容方法 return this.editor.getContent() } }, destroyed() { this.editor.destroy(); } } </script> 

4.使用

当我们需要使用富文本编辑器时,直接调用公共组件即可

<template> <div class="components-container"> <div class="info">UE编辑器示例<br>需要使用编辑器时,调用UE公共组件即可。可设置填充内容defaultMsg,配置信息config(宽度和高度等),可调用组件中获取内容的方法。</div> <button @click="getUEContent()">获取内容</button> <div class="editor-container"> <UE :defaultMsg=defaultMsg :config=config ref="ue"></UE> </div> </div> </template> <style> .info{ border-radius: 10px; line-height: 20px; padding: 10px; margin: 10px; background-color: #ffffff; } </style> <script> import UE from '../../components/ue/ue.vue'; export default { components: {UE}, data() { return { defaultMsg: '这里是UE测试', config: { initialFrameWidth: null, initialFrameHeight: 350 } } }, methods: { getUEContent() { let content = this.$refs.ue.getUEContent(); this.$notify({ title: '获取成功,可在控制台查看!', message: content, type: 'success' }); console.log(content) } } }; </script>

效果如下:
vue2.0项目中使用Ueditor富文本编辑器示例

5.报错

ESlint报错

eslint报错的参考请评论4L 5L

严格模式报错

部分人使用时出现以下报错:
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them...
这个问题是因为项目中的使用的babel默认添加了use strict造成,可参考 https://segmentfault.com/q/1010000007415253
我采用的是链接中答案的第三种方式:添加了babel-plugin-transform-remove-strict-mode,并在.babelrc里添加下列代码

{
  "plugins": ["transform-remove-strict-mode"] }

然后就没问题了。

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

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

(0)
上一篇 2021年10月11日 下午5:00
下一篇 2021年10月11日 下午5:00


相关推荐

  • resnet源码pytorch_pytorch conv1d

    resnet源码pytorch_pytorch conv1d#Pytorch 0.4.0 ResNet34实现cifar10分类.#@Time:2018/6/17#@Author:xfLiimporttorchvisionastvimporttorchastimporttorchvision.transformsastransformsfromtorchimportnnfromtorch.utils.da…

    2022年10月6日
    5
  • 如何计算时间复杂度

    如何计算时间复杂度一 概念时间复杂度是总运算次数表达式中受 n 的变化影响最大的那一项 不含系数 比如 一般总运算次数表达式类似于这样 a 2 n b n 3 c n 2 d n lg n e n fa 0 时 时间复杂度就是 O 2 n a 0 bO n 3 a b 0 cO n 2 依此类推 eg 1 for i 1 i 循环了 n n 次 当然是 O n 2

    2026年3月17日
    1
  • CS和BS_cs和bs架构的优缺点

    CS和BS_cs和bs架构的优缺点一、什么是CS和BS结构? 1.C/S又称Client/Server或客户/服务器模式。服务器通常采用高性能的PC、工作站或小型机,并采用大型数据库系统,如Oracle、Sybase、Informix或SQLServer。客户端需要安装专用的客户端软件。 2.B/S是Brower/Server的缩写,客户机上只要安装一个浏览器(Browser),如NetscapeNavigator或Intern…

    2025年10月17日
    5
  • ES6 判断对象是否为空「建议收藏」

    ES6 判断对象是否为空「建议收藏」判断对象是否为空最简单的方式就是用ES6的letobj={}if(Object.keys(obj).length==0){ console.log(“对象是空的”)}else{//Object.keys(obj).length可获取对象的长度console.log(“obj的长度:”+Object.keys(obj).length)}…

    2022年5月22日
    442
  • Cocos2d-x 水果忍者划痕效果

    Cocos2d-x 水果忍者划痕效果

    2021年11月14日
    42
  • AI 智能体开发全流程

    AI 智能体开发全流程

    2026年3月13日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号