vue+element-ui中引入编辑器
wangeditor编辑器
1.执行:npm install --save wangeditor
2.在你需要调用编辑器的vue文件中引入 wangeditor:
import E from 'wangeditor'
3.页面调用:
<div id="editor">
<!--<p>{{addFormData.content?'请输入正文':addFormData.content}} </p>-->
</div>
初始化编辑器:
在 mounted 方法中:
that.$nextTick(function () {
// Code that will run only after the
// entire view has been rendered
// 创建编辑器
that.editor = new E('#editor')
// 自定义菜单配置
that.editor.customConfig.menus = [
'bold',// 粗体
'italic',// 斜体
'head',// 标题
'quote', // 引用
'list', // 列表
'link', // 插入链接
'image', // 插入图片
'table', // 表格
]
that.editor.customConfig.onchange = function (html) {
if (html.replace(/<\/?[^>]+>/g, '')!=="") {
that.addFormData.content = html
that.$refs['addForm'].validateField('content')
}
// html 即变化之后的内容
// console.log(html)
}
// 配置服务器端地址
that.editor.customConfig.uploadImgServer = that.uploadUrl
// 将图片大小限制为 1M
that.editor.customConfig.uploadImgMaxSize = 1 * 1024 * 1024
// 限制一次最多上传 1 张图片
that.editor.customConfig.uploadImgMaxLength = 1
that.editor.customConfig.uploadFileName = 'file'; //设置文件上传的参数名称
that.editor.customConfig.uploadImgHooks = {
customInsert: function (insertImg, result, editor) {
// 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
// insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
// 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
if (result.code === 200) {
insertImg(that.imgSrc + result.data.id)
}
// var url = result.url
// insertImg(url)
// result 必须是一个 JSON 格式字符串!!!否则报错
}
}
that.editor.create()
that.editor.txt.html(that.addFormData.content ? that.addFormData.content : '')
// 禁用
if (that.addFormData.disabled) {
// that.editor.disable();
that.editor.$textElem.attr('contenteditable', false)
}
})
注意:
小颖之所以把初始化编辑器写在 $nextTick 内,是因为小颖调用编辑器时是在 el-dialog 组件中调用的,如果直接在 mounted 下初始化时,它始终不出来。
保存时:
that.addFormData.content = that.editor.txt.html()
that.btnLoading = true
if (that.addFormData.content.replace(/<\/?[^>]+>/g, '')==="") {
that.addFormData.content = ''
}
ckeditor5 编辑器
1.执行:npm install --save @ckeditor/ckeditor5-vue @ckeditor/ckeditor5-build-classic
2.在 main.js 中引入
import CKEditor from '@ckeditor/ckeditor5-vue';
Vue.use( CKEditor );
3.在你需要编辑器的vue文件中:
先引入所需文件
import ClassicEditor from '@ckeditor/ckeditor5-build-classic/build/ckeditor';
import '@ckeditor/ckeditor5-build-classic/build/translations/zh-cn';
import { MyCustomUploadAdapterPlugin } from "@/util/upload"
upload.js处理上传图片
import {constants} from "@/util/common";
class MyUploadAdapter {
constructor( loader ) {
// 要在上载期间使用的文件加载器实例
this.loader = loader;
}
// 启动上载过程
upload() {
return this.loader.file
.then( file => new Promise( ( resolve, reject ) => {
this._initRequest();
this._initListeners( resolve, reject, file );
this._sendRequest( file );
} ) );
}
// 中止上载过程
abort() {
if ( this.xhr ) {
this.xhr.abort();
}
}
// 使用传递给构造函数的URL初始化XMLHttpRequest对象.
_initRequest() {
const xhr = this.xhr = new XMLHttpRequest();
xhr.open( 'POST', constants.uploadUrl, true );
xhr.responseType = 'json';
}
// 初始化 XMLHttpRequest 监听.
_initListeners( resolve, reject, file ) {
const xhr = this.xhr;
const loader = this.loader;
const genericErrorText = `无法上传文件: ${ file.name }.`;
xhr.addEventListener( 'error', () => reject( genericErrorText ) );
xhr.addEventListener( 'abort', () => reject() );
xhr.addEventListener( 'load', () => {
const response = xhr.response;
// 当code==200说明上传成功,可以增加弹框提示;
// 当上传失败时,必须调用reject()函数。
if ( !response || response.error || response.code !== 200 ) {
return reject( response && response.msg ? response.msg : genericErrorText );
}
//上传成功,从后台获取图片的url地址
resolve( {
default: constants.downloadUrl + response.data.id
} );
} );
// 支持时上传进度。文件加载器有#uploadTotal和#upload属性,用于在编辑器用户界面中显示上载进度栏。
if ( xhr.upload ) {
xhr.upload.addEventListener( 'progress', evt => {
if ( evt.lengthComputable ) {
loader.uploadTotal = evt.total;
loader.uploaded = evt.loaded;
}
} );
}
}
// 准备数据并发送请求
_sendRequest( file ) {
//通过FormData构造函数创建一个空对象
const data = new FormData();
//通过append()方法在末尾追加key为files值为file的数据,就是你上传时需要传的参数,需要传更多参数就在下方继续append
data.append( 'file', file );//上传的参数data
// data.append( 'memberId', "666" );
/**
* 重要提示:这是实现诸如身份验证和CSRF保护等安全机制的正确位置。
* 例如,可以使用XMLHttpRequest.setRequestHeader()设置包含应用程序先前生成的CSRF令牌的请求头。
*/
this.xhr.send( data );
}
}
function MyCustomUploadAdapterPlugin( editor ) {
editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
// 在这里将URL配置为后端上载脚本
return new MyUploadAdapter( loader );
};
}
export {
MyUploadAdapter,
MyCustomUploadAdapterPlugin
}
upload.js
html调用:
<ckeditor :editor="editor" v-model="addFormData.content" :config="editorConfig" data:disabled="addFormData.disabled"></ckeditor>
data中配置相应参数:
//editor: {}
editor: ClassicEditor,
// editorData: '<p>Content of the editor.</p>',
editorConfig: {
// The configuration of the editor.
language: 'zh-cn',
extraPlugins: [ MyCustomUploadAdapterPlugin ],
toolbar: {
items: [
'heading',
'|',
'bold',
'italic',
'link',
'bulletedList',
'numberedList',
'|',
'indent',
'outdent',
'|',
'imageUpload',
'blockQuote',
'insertTable',
'undo',
'redo'
]
},
}
vue+element-ui中引入编辑器的更多相关文章
- vue+element ui中select组件选择失效问题原因与解决方法
codejing 2020-07-10 09:13:31 652 收藏 分类专栏: Web Vue Element UI 版权 .当表单form赋完值后,如果后续又对form中某一属性值进行操作如 ...
- vue + element ui 阻止表单输入框回车刷新页面
问题 在 vue+element ui 中只有一个输入框(el-input)的情况下,回车会提交表单. 解决方案 在 el-form 上加上 @submit.native.prevent 这个则会阻止 ...
- Vue+Element UI 实现视频上传
一.前言 项目中需要提供一个视频介绍,使用户能够快速.方便的了解如何使用产品以及注意事项. 前台使用Vue+Element UI中的el-upload组件实现视频上传及进度条展示,后台提供视频上传AP ...
- vue+element ui 的tab 动态增减,切换时提示用户是否切换
前言:工作中用到 vue+element ui 的前端框架,动态添加 Tab,删除 Tab,切换 Tab 时提示用户是否切换等,发现 element ui 有一个 bug,这里记录一下如何实现.转载 ...
- Vue+element ui table 导出到excel
需求: Vue+element UI table下的根据搜索条件导出当前所有数据 参考: https://blog.csdn.net/u010427666/article/details/792081 ...
- 分享一个自搭的框架,使用Spring boot+Vue+Element UI
废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...
- Element UI 中组件this.$message报错
最近在做毕设的时候,用Element UI中的消息提示message一直报以下的错误: 展示的效果也不好看,没有图标什么的: 但我明明有在main.js引入了element-ui 呀,因为毕设时间很赶 ...
- 关于Element UI中页面样式小问题
一,修改组件dialog窗口的大小 二,在我使用upload组件上传一张美女图片时,发现当预览图片时,图片是灰色的,点击一下才会变亮,这种效果使我很不舒服,于是我通过添加下面的一条样式,问题解决了(可 ...
- vue + element ui 实现实现动态渲染表格
前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...
- vue + element ui 表格自定义表头,提供线上demo
前言:工作中用到 vue+element ui 的前端框架,需要使用自定义表头,需要使用 re.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9710826.h ...
随机推荐
- Hadoop集群启动没有DataNode进程
问题状况: 问题原因: 在启动Hadoop之前,进行了多次格式化,导致DataNode的ID发生了变化 解决方案: 我们可以删除从节点所有的DataNode资料,并重新格式化 解决流程 1.根据cor ...
- [kubernetes]二进制部署k8s集群
0. 前言 采用二进制部署三主三工作节点的k8s集群,工作节点和Master节点共用服务器,因此只用到了三台服务器.master采用haproxy + keepalive实现高可用.实际生产环境中,建 ...
- cpu,gpu的种类
- 用Python语言进行多元时间序列ARIMAX模型分析
1.ARIMAX模型定义 ARIMAX模型是指带回归项的ARIMA模型,又称扩展的ARIMA模型.回归项的引入有利于提高模型的预测效果.引入的回归项一般是与预测对象(即被解释变量)相关程度较高的变量. ...
- 《深入理解Java虚拟机》读书笔记: 类加载器
类加载器 虚拟机设计团队把类加载阶段中的"通过一个类的全限定名来获取描述 ...
- 《Kali渗透基础》01. 介绍
@ 目录 1:渗透测试 1.1:安全问题的根源 1.2:安全目标 1.3:渗透测试 1.4:标准 2:Kali 2.1:介绍 2.2:策略 2.3:安装 3:Kali 初步设置 3.1:远程连接 3. ...
- VulnStack - ATT&CK红队评估实战(一) Writeup
VulnStack - ATT&CK红队评估实战(一) Writeup VulnStack(一)环境搭建 项目地址 http://vulnstack.qiyuanxuetang.net/vul ...
- [编程基础] Python内置模块collections使用笔记
collections是Python标准库中的一个内置模块,它提供了一些额外的数据结构类型,用于增强Python基础类型如列表(list).元组(tuple)和字典(dict)等.以下是对collec ...
- 【krpano】 ASP说一说插件
简述 这是一个Asp版krpano说一说案例,运用asp+jquery读写存储入xml文件数据库,结合krpano代码实现的功能:现将案例上传网站供大家学习研究,希望对大家有所帮助. 功能 用户可在网 ...
- 我的 Windows 文件管理哲学
前言 作为一个不合格的 Geek,我经常面临把 Windows 弄崩溃的尴尬处境,我的系统因此重装了一遍又一遍--不过在一次次的重装中,我逐渐总结出了于我个人而言行之有效的文件管理哲学,在此略做总 ...