>  기사  >  웹 프론트엔드  >  nodejs+mongodb+vue를 사용하여 ueditor를 구성하는 방법

nodejs+mongodb+vue를 사용하여 ueditor를 구성하는 방법

亚连
亚连원래의
2018-06-14 10:29:201177검색

이 글에서는 주로 nodejs+mongodb+vue 프론트엔드와 백엔드에서 ueditor를 구성하기 위한 샘플 코드를 소개하고 있으니 참고하시기 바랍니다.

개인 블로그 프로젝트를 할 때 배경과 상호작용하기 위한 리치 텍스트 박스 입력 컴포넌트가 필요했는데 nodejs에 대한 공식적인 구성이 없어서 직접 정보를 확인하고 조사해서 최종적으로 적용했습니다. 시스템.

1. 백엔드 구성

우선 이 프로젝트를 찾았습니다: https://github.com/netpi/ueditor 해당 오픈 소스 코드를 사용하여 노드에 ueditor를 적용할 수 있습니다.

1. 먼저 종속성 설치:

npm install ueditor --save

2. 노드 설정 구성

//引入接口文件
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......')

여기서 주목해야 할 점은 ueditor가 필요하기 때문에 플러그인이 node_module에 설치되어 있으므로 필요하지 않습니다. 추가 파일을 복사하려면 백엔드로 반환된 데이터를 저장할 디렉터리 아래에 새 공용 폴더를 생성하기만 하면 됩니다. 또한 구성 파일 config.json

도 도입해야 합니다. 2. 프런트엔드 구성

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/"

그런 다음 컴포넌트에서 구성합니다

My 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"
  };
 },

You ueditor의 기본 기능을 성공적으로 구성할 수 있습니다

Three. Front 및 Backend 요청 프록시

Vue 개발 환경에서는 webpack의 ProxyTable을 설정하여 백엔드 요청 프록시를 전달할 수 있으며, 파일 업로드 기능을 쉽게 디버깅할 수 있습니다. 마찬가지로, vue 빌드 이후의 파일은 정적 파일을 백엔드와 동일한 포트로 프록시해야 합니다. 그래야만 백엔드 포트를 요청할 수 있습니다

위 내용이 모든 사람에게 도움이 되기를 바랍니다. 미래에.

관련 기사:

js의 파일 로딩 최적화 관련 문제

Node.js에서 작은 프로그램 백그라운드 서비스를 구축하는 방법

vue를 사용하여 ueditor 및 노드 백그라운드 구성을 도입하는 방법

위 내용은 nodejs+mongodb+vue를 사용하여 ueditor를 구성하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.