vue项目引入富文本编辑,图片上传/视频上传

本文档详细介绍了如何在Vue项目中引入富文本编辑器vue-quill-editor,并实现图片大小调整功能。同时,文章还涵盖了视频上传的配置,通过video.js接入,以及在组件和父组件中的应用步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:下载富文本和更改图片大小

  • npm install quill-image-resize-module --save//更改img图片大小
  • npm install vue-quill-editor –save//下载富文本编辑

2:在main.js中引入

import  VueQuillEditor from 'vue-quill-editor'//编辑器
// 样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(VueQuillEditor)

3:在vue.config.js中配置

module.exports = {
   
configureWebpack: {
   
plugins: [
      new webpack.ProvidePlugin({
   
        "window.Quill": "quill/dist/quill.js",
         Quill: "quill/dist/quill.js",
      }),
      ]
}
}

4.视频接入 video.js

import Quill from "quill";

const BlockEmbed = Quill.import('blots/block/embed')
class VideoBlot extends BlockEmbed {
   
  static create (value) {
   
    let node = super.create()
    node.setAttribute('src', value.url)
    node.setAttribute('controls', value.controls)
    node.setAttribute('width', value.width)
    node.setAttribute('height', value.height)
    node.setAttribute('webkit-playsinline', true)
    node.setAttribute('playsinline', true)
    node.setAttribute('x5-playsinline', true)
    return node;
  }
 
  static value (node) {
   
    return {
   
      url: node.getAttribute('src'),
      controls: node.getAttribute('controls'),
      width: node.getAttribute('width'),
      height: node.getAttribute('height')
    };
  }
}
VideoBlot.blotName = 'video'
VideoBlot.tagName = 'video'
VideoBlot.className = 'ql-video'
export default VideoBlot;

5:在组件中编写

<template>
    <div>
        <!-- 图片上传组件辅助 -->
        <el-upload :action="uploadImgUrl" ref="uploadFileImage" :data="imgCol" :before-upload="handleBeforeUpload"
            :on-success="handleUploadSuccess" :on-error="uploadError" name="file" :show-file-list="false"
            :headers="headers" style="display: none" />
             <!--视频上传组件辅助 -->
        <el-upload :action="uploadImgUrl" ref="uploadFileVideo" :data="imgColVideo"
            :before-upload="handleBeforeUploadVideo" :on-success="handleUploadSuccessVideo"
            :on-error="handleUploadErrorVideo" name="file" :show-file-list="false" :headers="headers"
            style="display: none" />
        <!
### 如何在 Vue.js 中添加和配置富文本编辑器组件 #### 使用 `@wangeditor/editor-for-vue` 实现富文本编辑器 为了在 Vue 3 项目中集成富文本编辑器,可以采用 `@wangeditor/editor-for-vue` 这一库来构建支持图片上传、双向数据绑定等功能的组件[^1]。 安装依赖: ```bash npm install @wangeditor/editor @wangeditor/editor-for-vue ``` 引入并注册组件,在项目的入口文件或相应页面加入如下代码: ```javascript import { createApp } from 'vue'; import App from './App.vue'; // 导入样式 import '@wangeditor/editor/dist/css/style.css'; const app = createApp(App); app.use(/* ... */); export default { name: "MyComponent", components: {}, }; ``` 定义组件模板部分,设置容器用于承载编辑区域: ```html <div id="editor-container"></div> <wang-editor v-model="content" /> ``` 编写脚本逻辑处理编辑器初始化及其他交互行为: ```javascript <script setup> import WangEditor from "@wangeditor/editor-for-vue"; import { ref, onBeforeUnmount } from "vue"; let editor; const content = ref("<p>初始内容</p>"); function handleCreated(editorInstance) { editor = editorInstance; // 记录下 editor 实例,后面可以用 editor.api 获取编辑器接口 } defineExpose({ setContent(value) { editor?.setText(value); }, }); </script> <template> <wang-editor :value="content" @on-created="handleCreated"/> </template> ``` #### 利用 `Vue-Quill-Editor-Upload` 构建增强型富文本编辑器 另一种方案是借助于 `Vue-Quill-Editor-Upload` 来搭建具备更多特性的富文本编辑环境。此插件不仅提供基本的文字排版能力,还增强了对于媒体资源的支持程度,比如允许直接嵌入图像与视频素材等[^2]。 先执行必要的包管理命令获取所需模块: ```bash npm i vue-quill-editor axios form-data --save ``` 接着按照官方文档说明完成具体操作步骤即可快速上手使用该控件。 #### 应用百度 UEditor 组件化解决方案 针对某些特定场景下的应用需求,也可以考虑选用由国内知名搜索引擎公司提供的开源产品——百度 UEditor 。其特点是兼容性强、稳定性高,并且拥有丰富的API可供调用定制个性化功能[^3]。 安装过程相对简单明了: ```bash npm install baidu-web-uieditor --save ``` 随后参照给出的例子调整HTML结构以及JavaScript语句片段从而顺利启动实例对象。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值