vue-ueditor-wrap安装、封装及使用
安装 vue ueditor
npm install vue-ueditor-wrap
下载已处理的 UEditor
进入此链接下载对应版本
该文章以
jsp
版本为例
将下载的压缩包解压后重命名为 UEditor
,放入 vue
项目的 public
文件夹中,如下图所示:
将 jsp/config.json
中的内容放入 ueditor.config.js
,放在 toolbars
之后即可:
修改配置中的上传文件的名称以及 action
此处上传文件名称即为 发起请求是传文件参的参数名称, 一定要与上传接口对应上!
action
即url
后缀的/?action=uploadimage
此样式同时也要注意配置文件中的 访问路径前缀
上传接口注意事项
UEditor
要求上传接口返回的参数中包含state
,url
,title
,original
当返回的
state
为SUCCESS
时,即为上传成功,表示上传的状态
url
表示该文件真实的url
地址
title
代表该文件在标签中的title
字段
original
代表该文件在标签中的alt
字段
Controller
例子:
其中包含了自己项目中的内容,这仅仅是一个展示返回字段的例子
@PostMapping("/uploadTest")
public String uploadFileTest(MultipartFile file) throws Exception {
try {
// 上传文件路径
String filePath = NiuaConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileName;
redisTemplate.opsForValue().set("uploadUrl", url);
ObjectMapper mp = new ObjectMapper();
UEResJson neo = new UEResJson();
neo.setState("SUCCESS");
neo.setUrl(url); // url
neo.setTitle(fileName); // title
neo.setOriginal(fileName); // alt
String str = mp.writeValueAsString(neo);
return str;
} catch (Exception e) {
return "";
}
}
该实体类中,除此之外,也可选
type
,size
字段
import lombok.Data;
import java.io.Serializable;
@Data
public class UEResJson implements Serializable {
private String state;
private String url;
private String title;
private String original;
}
封装 UE 子组件
在 components
文件夹中新建 UEditor
文件夹,并在其中创建 UE.vue
文件
UE.vue
内容如下:
注意
config
中的UEDITOR_HOME_URL
以及serverUrl
UEDITOR_HOME_URL
即下载的配置文件的路径
serverUrl
即上传模块的通用接口
<template>
<div>
<VueUeditorWrap v-model="content" :config="config"></VueUeditorWrap>
<el-button typeof="primary" @click="showContent()">Get UE Content</el-button>