1、 下载包:html
从Ueditor的官网下载1.4.3.3jsp版本的Ueditor编辑器,官网地址为:http://ueditor.baidu.com/website/download.html前端
下载解压后会获得若是下文件目录:vue
将上述Ueditor文件夹拷贝到vue项目的static文件夹中,此文件夹为项目的静态服务文件夹;node
2、 修改配置webpack
在ueditor.config.js中修改以下代码:web
// 这里是配置Ueditor内部进行文件请求时的静态文件服务地址express
window.UEDITOR_HOME_URL = "/static/Ueditor/" var URL = window.UEDITOR_HOME_URL || getUEBasePath();
3、 文件的引入npm
在vue项目的入口文件main.js中将Ueditor全部的基础文件引入以下:(路径自行配制)json
import'../static/Ueditor/ueditor.config.js' import'../static/Ueditor/ueditor.all.min.js' import'../static/Ueditor/lang/zh-cn/zh-cn.js' import'../static/Ueditor/ueditor.parse.min.js'
4、 在相应vue的componnent文件中使用富文本编辑器api
<template> <div> <!--editor的div为富文本的承载容器--> <div id="editor"></div> <button type="" @click="gettext">点击</button> </div> </template> <script> exportdefault { data() { return { editor: null, } }, mounted() { // 引入url UEDITOR_CONFIG.UEDITOR_HOME_URL = '../../static/Ueditor/' // 实例化editor编辑器 this.editor = UE.getEditor('editor'); // console.log(this.editor.setContent("1223")) }, methods: { gettext() { // 获取editor中的文本 console.log(this.editor.getContent()) } }, destroyed() { // 将editor进行销毁 this.editor.destroy(); } } </script>
5、 执行上述代码可能会出现的问题
出现此种现象的缘由是配置ueditor的图片以及文件的后台上传接口不正确;
若是Ueditor不须要使用文件以及图片的上传则在ueditor.config.js中进行以下配置:(将serverUrl注释掉)
// 服务器统一请求接口路径 // serverUrl: URL + "jsp/controller.jsp",
之后将不会再出现上述报错,可是也将没法进行图片的上传:以下图:
若是Ueditor须要使用文件以及图片的上传则在ueditor.config.js中进行以下配置:
// 服务器统一请求接口路径,配置的服务端接口 serverUrl: "http://127.0.0.1:9999/api/UE", //或者若是使用了代理,则能够以下进行配置 serverUrl: "/api/ue",
6、 若是使用的是node的express作服务端,接口开发以下
首先下载编辑器包
npm install –save-dev ueditor
服务端项目文件中在public中增长以下目录以及文件
注:ueditor中的images文件夹是上传图片后存储的地方
nodejs中的config.js就是下载的ueditor包的jsp文件夹下config.json文件
开发接口
//加载ueditor 模块 var ueditor = require("ueditor"); //使用模块 app.use("/api/ue", ueditor(path.join(__dirname, 'public'), function(req, res, next) { // ueditor 客户发起上传图片请求 if (req.query.action === 'uploadimage') { var foo = req.ueditor; var imgname = req.ueditor.filename; var img_url = '/ueditor/images/'; res.ue_up(img_url); //你只要输入要保存的地址。保存操做交给ueditor来作 res.setHeader('Content-Type', 'text/html'); //IE8下载须要设置返回头尾text/html 否则json返回文件会被直接下载打开 } // 客户端发起图片列表请求 elseif (req.query.action === 'listimage') { var dir_url = '/ueditor/images/'; res.ue_list(dir_url); // 客户端会列出 dir_url 目录下的全部图片 } // 客户端发起其它请求 else { console.log('config.json') res.setHeader('Content-Type', 'application/json'); res.redirect('/ueditor/nodejs/config.js'); } }));
注:
上述接口中的"/api/ue"接口就是配置在前台项目ueditor.config.js文件中的serverUrl地址;
上述接口中img_url的'/ueditor/images/'和res.redirect的'/ueditor/nodejs/config.js'配置都是使用的express静态文件服务对图片存储路径和图片默认配置文件的存储和请求;
进行上述配置后,必定要在webpack的代理中添加以下代理:
// 配置ueditor的图片上传服务器预览路径 '/ueditor': { //后台接口地址 target: 'http://localhost:9999', //这里能够模拟服务器进行get和post参数的传递 changeOrigin: true, //前端全部的/ueditor'请求都会请求到后台的/ueditor'路径之下 pathRewrite: { '^/ueditor': '/ueditor' } }