首頁  >  文章  >  web前端  >  使用nodejs+mongodb+vue如何設定ueditor

使用nodejs+mongodb+vue如何設定ueditor

亚连
亚连原創
2018-06-14 10:29:201177瀏覽

本篇文章主要介紹了nodejs mongodb vue前後台配置ueditor的範例程式碼,現在分享給大家,也給大家做個參考。

筆者在做一個個人部落格專案的時候需要一個富文本框輸入元件與後台進行交互,但是官方配置裡面沒有關於nodejs的,於是自己查閱資料研究了一下,最後終於應用到了系統中。

一、後台配置

首先是找到了這個專案:https://github.com/netpi/ueditor,可以透過他開源的程式碼將ueditor應用的node上面,大概方法如下:

1.先安裝依賴:

npm install ueditor --save

2. 配置Node設定

//引入接口文件
const api = require('./api');
//引入文件模块
const fs = require('fs');
//引入处理路径模块
const path = require('path');
//引入处理post数据模块
var bodyParser = require('body-parser');

//引入express
const express = require('express');
const app = express();
//引入ueditor
const ueditor = require("ueditor")

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

//更改限定大小
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
// parse application/json
app.use(bodyParser.json())
app.use(api)

app.use("/ueditor/ue", ueditor(path.join(__dirname, 'public'), function(req, res, next) {
  //客户端上传文件设置
  var imgDir = '/img/ueditor/'
  var ActionType = req.query.action;
  if (ActionType === 'uploadimage' || ActionType === 'uploadfile' || ActionType === 'uploadvideo') {
    var file_url = imgDir; //默认图片上传地址
    /*其他上传格式的地址*/
    if (ActionType === 'uploadfile') {
      file_url = '/file/ueditor/'; //附件
    }
    if (ActionType === 'uploadvideo') {
      file_url = '/video/ueditor/'; //视频
    }
    res.ue_up(file_url); //你只要输入要保存的地址 。保存操作交给ueditor来做
    res.setHeader('Content-Type', 'text/html');
  }
  // 客户端发起图片列表请求
  else if (req.query.action === 'listimage') {
    var dir_url = imgDir;
    res.ue_list(dir_url); // 客户端会列出 dir_url 目录下的所有图片
  }
  // 客户端发起其它请求
  else {
    // console.log('config.json')
    res.setHeader('Content-Type', 'application/json');
    res.redirect('../nodejs/config.json');
  }
}));

//处理静态文件 todo
// 访问静态资源文件 这里是访问所有dist目录下的静态资源文件
app.use(express.static(path.resolve(__dirname, 'public/')))
app.use('/ueditor', function(req, res) {
  res.render('views/');
});

//监听8888端口
app.listen(8888);
console.log('sucess listen......')

這裡要注意的是因為已經require了ueditor,所以該插件已經安裝到了node_module內,所以不需要再拷貝額外的文件了,只需要需要在這個目錄下面新建public資料夾存放返回給後台的數據,另外,還需要引入配置文件config.json

二、前台配置

vue的前台配置需要下載ueditor的檔案放在目錄中,我將其放在了static資料夾中,在vue入口檔案中引入ueditor的檔案:

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'

值得一提的是需要將ueditor.config.js檔案中的目錄配置為放置該插件的目錄:

window.UEDITOR_HOME_URL = "/static/UE/"

然後在元件中配置好就可以了

我的UE.vue元件:

<template>
 <script :id=id type="text/plain"></script>
</template>

<script>
 export default {
  name: &#39;UE&#39;,
  data () {
   return {
    editor: null
   }
  },
  props: {
   defaultMsg: {
    type: String
   },
   config: {
    type: Object
   },
   id: {
    type: String
   },
  },
  mounted() {
   const _this = this;
   this.editor = UE.getEditor(this.id, 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>

引入方式:

<UE :defaultMsg=defaultMsg :config=config :id=ue1 ref="ue"></UE>

data() {
  return {
   defaultMsg: "",
   image: "",
   config: {
    initialFrameWidth: null,
    initialFrameHeight: 350
   },
   ue1: "ue1"
  };
 },

就可以成功配置好ueditor的基本功能了

三、前後台請求代理程式

在vue dev環境下可以設定webpack的proxyTable將後端請求代理程式轉發,就可以輕鬆偵錯檔案上傳功能了,同理,vue build之後的檔案則需要用Node將靜態檔案代理到和後端同一個連接埠上才可以請求後台連接埠

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

在js中有關檔案載入最佳化方面的問題

在Node.js中如何建立小程式後台服務

使用vue如何引入ueditor及node後台設定

以上是使用nodejs+mongodb+vue如何設定ueditor的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn