Home Web Front-end JS Tutorial How to introduce ueditor and node background configuration using vue

How to introduce ueditor and node background configuration using vue

Jun 13, 2018 pm 06:10 PM
nodejs vue.js Configuration

This article mainly introduces the detailed explanation of the introduction of ueditor and node background configuration in vue. Now I will share it with you and give you a reference.

Recently, a customer of the company wanted to use our company's products. He had a lot of requirements for a rich text editor in his backend management. He initially planned to use Quill, but found that Quill could not meet his needs at all. After researching the rich text editors on the market, it seems that only Baidu’s ueditor is left. Although it's ugly~~~, it doesn't matter. The government website and this effect are more

vue introduces ueditor

Steps

  1. Baidu ueditor download, any version is fine (this article takes the PHP version as an example), if you don’t need particularly comprehensive functions, you can consider UM

  2. Place the corresponding folder in In static

  3. Modify ueditor.confg.js referenced by the front-end vue part and set the path 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"
 // ............ 下面忽略................
Copy after login

When writing vue files, I have configured static into the path of webpack. I can change it accordingly. The various methods in ueditor can be found in the index.html of the Baidu ueditor package I downloaded.

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

<script>
export default {
 name: &#39;HelloWorld&#39;,
 data () {
 return {
  editor: null
 }
 },
 methods: {
 show () {
  console.log(this.editor.getContent())
 }
 },
 mounted () {
 require(&#39;static/utf8-php/ueditor.config.js&#39;)
 require(&#39;static/utf8-php/ueditor.all.min.js&#39;)
 require(&#39;static/utf8-php/lang/zh-cn/zh-cn.js&#39;)
 require(&#39;static/utf8-php/ueditor.parse.min.js&#39;)
 this.editor = window.UE.getEditor(&#39;editor&#39;)
 },
 destroyed () {
 this.editor.destroy()
 }
}
</script>
Copy after login

Note

  1. The path in step 3 must have the last "/"

  2. The serverUrl in step 3 is written as the corresponding server address

node back-end processing

express implementation

Someone on the Internet has already implemented the express version. Those who use express are lucky. However, it cannot be used directly when I use it directly. ": unexcepected" is reported in the browser. I changed his code so that it does not redirect when returning the configuration, but directly returns a jsonp and jsonp content. Set it to config.json under the php file in Baidu's ueditor package. Remember to use regular expressions or directly remove the comments by hand. There are no comments in json.
At this time, you may find that no error is reported, but the image upload error occurs and a 404 is reported. In fact, the image has been uploaded successfully, but it is not loaded correctly, because the returned path is only the path, not the complete URL, and the request will be made to the front-end service domain. (e.g. "http://localhost:8080/**"). At this time, modify "imageUrlPrefix" in config.json: "http://localhost:3000" to complete the image path. Solve the cross-domain problem yourself -----

  1. res.jsonp(config.json)

  2. Add imageUrlPrefix to config.json Backend domain

koa implementation

This is a relatively sophisticated library, and the generator writing method will be removed in v3. The generator is now It is gradually not supported, so use async writing method. I mainly use the await-busboy library to implement file processing.

Implementing Judgment

const ActionType = ctx.query.action
// 当ActionType为config时返回与express中一样的json
// 当为uploadimage或uploadfile时处理
处理上传
const parse = require(&#39;await-busboy&#39;)
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 === &#39;uploadimage&#39; && img_type.indexOf(path.extname(part.filename)) >= 0 ){
   filename = &#39;pic_&#39;+ (new Date()).getTime() + &#39;_&#39; + part.filename
   file_path = path.join(img_path, filename)
  } else if (ActionType === &#39;uploadfile&#39;){
   filename = &#39;file_&#39;+(new Date()).getTime()+&#39;_&#39;+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哦~~
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Use webpack to handle the problem of excessively large bundle.js files

How to fill in the default avatar in javascript

6 types of regular expressions in JavaScript (detailed tutorial)

The above is the detailed content of How to introduce ueditor and node background configuration using vue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

How to connect nodejs to mysql database How to connect nodejs to mysql database Apr 21, 2024 am 06:13 AM

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

How to deploy nodejs project to server How to deploy nodejs project to server Apr 21, 2024 am 04:40 AM

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

Can nodejs write front-end? Can nodejs write front-end? Apr 21, 2024 am 05:00 AM

Yes, Node.js can be used for front-end development, and key advantages include high performance, rich ecosystem, and cross-platform compatibility. Considerations to consider are learning curve, tool support, and small community size.

See all articles