wangEditor 编辑器 Vue 2.0 + Nodejs 配置

资料

  1. Vue2.0 版本的安装:https://www.wangeditor.com/v5/for-frame.html#使用
  2. 上传图片配置:https://www.wangeditor.com/v5/menu-config.html#上传图片

安装步骤

1.安装界面基础部分

vue 复制代码
<!-- 富文本编辑器 -->
<template>
  <div class="WangEditor" style="border: 1px solid #ccc">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="height: 500px; overflow-y: hidden;"
      v-model="html"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
    />
  </div>
</template>

<script>
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default {
    components: { Editor, Toolbar },
    data() {
      return {
        editor: null,
        html: '<p>hello</p>',
        toolbarConfig: {},
        editorConfig: { 
          placeholder: '请输入内容...',
        },
        mode: 'default', // or 'simple'
      }
    },
    mounted() {
      // 模拟 ajax 请求,异步渲染编辑器
      setTimeout(() => {
        this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
      }, 1500)
    },
    beforeDestroy() {
      const editor = this.editor
      if (editor == null) return
      editor.destroy() // 组件销毁时,及时销毁编辑器
    },
    methods: {
      // 编辑器实例创建完毕后回调
      onCreated(editor) {
        // 对象密封
        this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
      },
    },
  }
</script>

<style scoped lang="scss">
.WangEditor{
  // 去掉重影小叉
  ::v-deep .btn-close {
    svg {
      display: none;
    }
  }
}
</style>

2.配置上传服务

js 复制代码
editorConfig: { 
 placeholder: '请输入内容...',
  MENU_CONF: {
    uploadImage:{
      fieldName: 'images',
      server: 'http://localhost:3000/api/uploadImage', // 默认的是8081,这里要设置自己的IP和端口
      maxSize: 5 * 1024 * 1024, // 5MB
    }
  }
},

3.搭建后端Api

3-1. 设置静态文件,添加目录
js 复制代码
// 创建主应用
const app = express();

// 使 public/uploads 目录可以作为静态文件目录访问
app.use('/uploads', express.static(path.join(__dirname, 'public', 'uploads')));
3-2. 配置Multer
js 复制代码
// Multer 配置
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'public/uploads/'); // 上传文件的存储路径
  },
  filename: (req, file, cb) => {
    // 对文件名进行处理,确保文件名中的非英文字符不会出现乱码
    const sanitizedFilename = Date.now() + '-' + encodeURIComponent(file.originalname);
    cb(null, sanitizedFilename); // 使用 URL 编码后的文件名
  }
});

// 创建 multer 实例
const upload = multer({
  storage: storage,
  limits: { fileSize: 5 * 1024 * 1024 },  // 限制文件大小最大为5MB
}).single('images');  // 这里使用 single() 来接收单个文件,'images' 是表单字段名称
3-3. 配置图片上传api
js 复制代码
// 图片上传
app.post('/api/uploadImage', (req, res) => {
  // 使用 multer 中间件来处理文件上传
  upload(req, res, (err) => {
    if (err) {
      return res.status(500).json({
        errno: 1,
        message: '文件上传失败',
      });
    }

    const file = req.file;  // 上传的文件信息存储在 req.file 中

    if (!file) {
      return res.status(400).json({
        errno: 1,
        message: '没有上传文件',
      });
    }

    // 获取服务器的 IP 地址
    const ip = req.socket.localAddress === '::1' ? 'localhost' : req.socket.localAddress;
    const port = 3000; // 端口号
    const baseUrl = `http://${ip}:${port}/uploads/`; // 构建完整的 URL

    // 返回文件的URL路径
    res.status(200).json({
      errno: 0,
      data: {
        url: `${baseUrl}${file.filename}`,  // 上传后的文件路径
        alt: 'Uploaded Image',
        href: '',  // 可选的链接(点击图片时跳转)
      },
    });
  })
})

4.解决弹窗小叉重影

css 复制代码
<style scoped lang="scss">
.WangEditor{
  // 去掉重影小叉
  ::v-deep .btn-close {
    svg {
      display: none;
    }
  }
}
</style>
相关推荐
OpenTiny社区13 小时前
🎨 看完 GenUI SDK 源码我悟了!
前端·vue.js·github
mqcode14 小时前
你项目里的 axios,封对了吗?从裸用到生产级的四步进化
vue.js·axios
Linsk15 小时前
组件 = 模板 + 业务逻辑
java·前端·vue.js
前端啊18 小时前
告别 el-table 打印难题,vue3-print-el-table 来了!
前端·vue.js
AprChell20 小时前
低代码设计器和低代码设计引擎架构综述
前端·vue.js·低代码
Ruihong20 小时前
🎉 VuReact 1.9.0 发布,支持 Vue 3.4 defineModel 编译到 React
vue.js·react.js·面试
英勇无比的消炎药20 小时前
TinyRobot 源码深度分析:OpenTiny 的 AI 对话组件库
前端·vue.js·github
行者全栈架构师2 天前
UniApp集成vk-uview-ui组件库详解:打造高效UI开发体验
前端·vue.js
Csvn2 天前
Vue 3 defineModel 翻车实录:多个 v-model 绑定到底怎么写?
前端·vue.js
Momo__2 天前
VueUse createReusableTemplate —— 单文件组件内的模板复用神器
前端·vue.js