当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue引入ueditor及node后台配置详解

vue引入ueditor及node后台配置详解

2018年02月04日  | 移动技术网IT编程  | 我要评论

西米怎么煮,五租客,显存容量

最近公司的某个客户要用上我们公司的产品,他的后台管理里的富文本编辑器要求有点多,开始打算用quill,但是发现quill根本满足不了他的需求。在调研了市面上的富文本编辑器后,最后似乎只剩了百度的ueditor。虽然很丑~~~,不过没关系,政府网站和这种效果更搭:-d 我是不是说了什么(逃

vue引入ueditor

步骤

  1. 百度ueditor下载,随便哪个版本就好(本文章以php版为例),不需要特别全面功能的可以考虑下um喽
  2. 将对应的文件夹放到static中
  3. 修改前端vue部分引用的ueditor.confg.js,设置路径window.ueditor_home_url = "/static/utf8-php/"
window.ueditor_home_url = "/static/utf8-php/"

 var url = window.ueditor_home_url || getuebasepath();
 /**
  * 配置项主体。注意,此处所有涉及到路径的配置别遗漏url变量。
  */
 window.ueditor_config = {

  //为编辑器实例添加一个路径,这个不能被注释
  ueditor_home_url: url
  // 服务器统一请求接口路径
  , serverurl: "http://localhost:3000/ueditor/ue"
 // ............ 下面忽略................

编写vue文件,我已经把static配置到webpack的路径里了,自己可以相应更改,ueditor中的各项方法可以在自己下载的百度ueditor包的中找。

<template>
 <div class="hello">
 <script id="editor" type="text/plain"></script>
 <button @click="show">你敢点一下吗?</button>
 </div>
</template>

<script>
export default {
 name: 'helloworld',
 data () {
 return {
  editor: null
 }
 },
 methods: {
 show () {
  console.log(this.editor.getcontent())
 }
 },
 mounted () {
 require('static/utf8-php/ueditor.config.js')
 require('static/utf8-php/ueditor.all.min.js')
 require('static/utf8-php/lang/zh-cn/zh-cn.js')
 require('static/utf8-php/ueditor.parse.min.js')
 this.editor = window.ue.geteditor('editor')
 },
 destroyed () {
 this.editor.destroy()
 }
}
</script>

注意事项

  1. 在步骤3中的路径一定要有最后一个"/"
  2. 步骤3中的serverurl写成对应的服务端地址

node后端处理

express 实现

网上有人已经实现了express版的,使用express的有福了。不过我直接用他的是不能直接用的,在浏览器中报": unexcepected ",我将他的代码改了一下,不让它在返回配置是重定向,而是直接返回一个jsonp,jsonp内容设置为百度的ueditor包中的php文件下的config.json,记得用正则表达式或者直接用手把注释去掉,json是没有注释的。
这时你可能发现不报错了,但是图片上传会错误,报404。其实图片已经上传成功了,只是没有正确的加载,因为返回的路径只是路径,不是完整的url,就会请求到前端服务域下。(例如"http://localhost:8080/**")。此时修改config.json中"imageurlprefix": "http://localhost:3000",就可以将图片路径补充完整。跨域问题自己解决哈-----

  1. res.jsonp(config.json)
  2. 给config.json的imageurlprefix加后端域

koa实现

这时个比较精巧的库,而且将在v3中去掉了generator写法,generator现在已经渐渐不被支持,所以使用async写法吧。我主要用了await-busboy这个库,实现文件处理。

实现判断

const actiontype = ctx.query.action
// 当actiontype为config时返回与express中一样的json
// 当为uploadimage或uploadfile时处理
处理上传
const parse = require('await-busboy')
const parts = parse(ctx)
 let part,
  stream,
  tmp_name,
  file_path,
  filename
 while ((part = await parts)) {
  if (part.length) {
   // 此处解析到form的fields
   console.log({ key: part[0], value: part[1] })
  } else {
  // 此处解析到文件并以可读流形式返回,通过nodejs官方api存储
  if(actiontype === 'uploadimage' && img_type.indexof(path.extname(part.filename)) >= 0 ){
   filename = 'pic_'+ (new date()).gettime() + '_' + part.filename
   file_path = path.join(img_path, filename)
  } else if (actiontype === 'uploadfile'){
   filename = 'file_'+(new date()).gettime()+'_'+part.filename
   file_path = path.join(files_path, filename)
  }
  stream = fs.createwritestream(path.join(static_path,file_path))
  part.pipe(stream)
  tmp_name = part.filename
 }
 // 返回json要引用koa-jsonp哦~~

到这大概可以了,自己去试一下吧~~希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网