【前端】VUE3引用 wangeditor ^4.7.15富文本

文章介绍了如何在Vue项目中安装并配置WangEditor富文本编辑器,包括引入组件、设置上传图片功能、处理父子组件间的值传递,以及在父组件中引用和调用编辑器方法。
Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

安装:

cnpm i wangeditor@4.7.15   -S

components文件夹下新建一个Vue组件:

<template>
    <div>
        <div id="websiteEditorElem" style="height:300px;background: #ffffff;"></div>
    </div>
</template>
 
<script>
 import { getToken } from "@/utils/auth";
import EWangEditor from "wangeditor";
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const uploadFileUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload"); // 上传文件服务器地址

export default {
    name: "wangEditor",
    props: {
        content: "" //获取从父组件中传过来的值,主要用于修改的时候获取值,并加入到富文本框中
    },
    data() {
        return {
            phoneEditor: '',
            name: '',
        }
    },
    methods: {
 
    },
    mounted() {
            this.phoneEditor = new EWangEditor('#websiteEditorElem')
            // true 文件采用base64保存  false:文件采用地址方式保存
            const b = false;
            // 图片以base64形式保存
            this.phoneEditor.config.debug = true;
            if (b) {
                this.phoneEditor.config.uploadImgShowBase64 = true
            }else{
                // 配置上传图片功能
                this.phoneEditor.config.uploadImgServer = uploadFileUrl.value //填写后台服务器地址
                this.phoneEditor.config.uploadFileName = 'file' // 后端接受上传文件的参数名
                this.phoneEditor.config.uploadImgMaxSize = 30 * 1024 * 1024 // 限制上传图片的大小为3MB
                this.phoneEditor.config.uploadImgMaxLength = 600 // 限制一次最多上传 6 张图片
                this.phoneEditor.config.uploadImgTimeout = 40 * 60 * 1000 // 设置超时时间
                this.phoneEditor.config.uploadImgParams = {file_type: 'img'}
                this.phoneEditor.config.uploadImgHeaders = {Authorization: "Bearer " + getToken()}
                this.phoneEditor.config.uploadImgHooks = {
                    customInsert: function (insertImg, result, editor) {
                        if (result.code === 200) {
                        // 插入图片到编辑器中
                        insertImg(baseUrl+result.fileName) 
                        } else {
                            console.error('上传图片失败')
                        }
                    }
                }
            }
            this.phoneEditor.config.zIndex = 100;
            this.phoneEditor.config.pasteFilterStyle = false;
            // 创建一个富文本编辑器
            this.phoneEditor.create()
            // 修改的时候,需要富文本内容回显,则需要加入以下代码
            this.phoneEditor.txt.html(this.modelValue)
            this.phoneEditor.config.onchange = (html) => {
                this.info_ = html // 绑定当前逐渐地值
                this.$emit('change', this.info_) // 将内容同步到父组件中
            }
    },
}
</script>

父组件引用:

页面:
 <el-form-item label="内容">
          <WangEdit @change="grtUrl" :content="editForm.content"></WangEdit>
 </el-form-item>
JS:
<script>
import WangEdit from '@/components/wangEditor.vue' //WangEditor在项目中的地址
export default{
    components:{ WangEdit },
    data(){
        return{
            editForm:{content:''},
         }
    }methods:{
        grtUrl(path){
            this.editForm.content = path;
        },
        saveOrUpdate(){
            console.log('send',this.editForm)
            //注释掉的这行代码就是控制页面显示是否带<p>标签的
            //this.editForm.content=this.editForm.content.replace(/<[^>]+>/g,"")
            //todo  以下可进行后端接口保存调用...
        },
    }
}
</script>

尾声:

有问题可看视频教学 官方网站

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

<think>好的,用户想在Vue2中使用wangeditor 4.7.15版本创建支持图片上传的富文本编辑器,用于商品详情页。之前我已经给出过基于较新版本的实现方案,但用户现在明确指定要使用4.7.15,需要调整之前的回答以适配旧版本。 首先,我需要回忆wangeditor v4和v5之间的主要区别。v5版本可能使用了不同的包结构,比如@wangeditor/editor-for-vue,而v4.7.15可能还在使用旧版的安装方式,比如wangeditor-reborn或原生的wangeditor。根据用户提供的引用,他们在Vue3中使用的是@wangeditor/editor-for-vue@next,而Vue2使用的是非next版本。但用户现在需要的是4.7.15,可能需要确认正确的安装包。 查看用户提供的引用[2]中提到,Vue2安装的是@wangeditor/editor-for-vue,而Vue3是@next版本。但引用[3]中的代码显示使用的是editor1和editor2,这可能涉及到多个编辑器实例,需要注意组件销毁时的处理。同时,用户提到要使用4.7.15版本,可能需要安装特定的旧版本包,例如wangeditor而不是@wangeditor/editor。 接下来,我需要检查wangeditor的版本历史。wangeditor在v5之前可能包名不同,可能之前是叫做wangeditor,而v5之后改为@wangeditor/editor。因此,用户要使用4.7.15的话,正确的安装命令应该是npm install wangeditor@4.7.15,而不是@wangeditor/editor。同时,Vue2的集成方式可能有所不同,可能需要手动创建组件,而不是使用官方提供的Vue组件包。 根据用户引用[3]中的代码,他们是在Vue2中使用组件的方式,可能使用的是旧版的方式,即直接引入wangeditor的JS文件,或者通过Vue组件封装。需要确认在4.7.15版本中,是否支持Vue组件封装,或者是否需要使用传统的DOM操作方式。 另外,图片上传的配置在v4和v5之间可能有差异。在v4中,可能需要使用customUpload配置,而v5中可能是通过server属性设置。用户之前的回答中提到了两种配置方式:一种是通过server指定接口,另一种是自定义customUpload函数。需要确认在4.7.15版本中,图片上传的正确配置方式。 用户引用[1]中提到在editorConfig中添加MENU_CONF,这可能在v4中适用。因此,需要调整之前的配置,确保上传部分正确。同时,注意在旧版本中可能需要使用不同的钩子函数或配置项。 最后,需要确保组件的销毁处理,避免内存泄漏。在Vue2中,使用beforeDestroy生命周期钩子来销毁编辑器实例。 总结步骤: 1. 安装正确的包版本:wangeditor@4.7.15。 2. 创建Vue组件,手动初始化编辑器。 3. 配置图片上传,使用customUpload或server属性。 4. 处理双向数据绑定,通过v-model或事件传递内容。 5. 销毁编辑器实例,避免内存泄漏。 需要验证这些步骤的正确性,确保在Vue2和wangeditor 4.7.15下的兼容性。</think>根据wangeditor v4.x版本的特性,以下是Vue2项目中使用4.7.15版本实现带图片上传功能的完整方案: ### 一、环境配置 ```bash # 安装指定版本 npm install wangeditor@4.7.15 --save ``` ### 二、基础组件封装 新建`components/WangEditor.vue`: ```vue <template> <div class="editor-box"> <div ref="toolbar" class="toolbar"></div> <div ref="editor" class="editor"></div> </div> </template> <script> import E from &#39;wangeditor&#39; export default { model: { prop: &#39;value&#39;, event: &#39;change&#39; }, props: { value: { type: String, default: &#39;&#39; } }, data() { return { editor: null } }, watch: { value(val) { if (val !== this.editor.txt.html()) { this.editor.txt.html(val) } } }, mounted() { this.initEditor() }, methods: { initEditor() { const editor = new E(this.$refs.toolbar, this.$refs.editor) // 图片上传配置 editor.customConfig = { onchange: html => { this.$emit(&#39;change&#39;, html) }, uploadImgServer: &#39;/api/upload&#39;, // 上传接口 uploadFileName: &#39;file&#39;, // 文件参数名 uploadImgMaxSize: 3 * 1024 * 1024, // 3MB uploadImgAccept: [&#39;jpg&#39;, &#39;jpeg&#39;, &#39;png&#39;, &#39;gif&#39;], uploadImgHooks: { customInsert: (insertImgFn, result) => { if (result.code === 0) { insertImgFn(result.data.url) } else { this.$message.error(result.msg || &#39;上传失败&#39;) } } } } // 创建编辑器 this.editor = editor.create() this.editor.txt.html(this.value) } }, beforeDestroy() { if (this.editor) { this.editor.destroy() this.editor = null } } } </script> <style scoped> .editor-box { border: 1px solid #ccc; } .toolbar { border-bottom: 1px solid #ccc; } .editor { height: 500px; overflow-y: auto; } </style> ``` ### 三、在商品详情页中使用 ```vue <template> <div class="goods-detail"> <wang-editor v-model="detailContent"/> </div> </template> <script> import WangEditor from &#39;@/components/WangEditor&#39; export default { components: { WangEditor }, data() { return { detailContent: &#39;&#39; // 绑定商品详情内容 } } } </script> ``` ### 四、关键配置解析(v4.x特性) 1. **图片上传核心配置**: ```js editor.customConfig = { uploadImgServer: &#39;你的上传接口&#39;, // 必填 uploadFileName: &#39;file&#39;, // 文件参数名 uploadImgParams: { // 额外参数 token: &#39;your-upload-token&#39; }, uploadImgMaxSize: 3 * 1024 * 1024, // 文件大小限制 uploadImgHeaders: { // 自定义header &#39;X-Requested-With&#39;: &#39;XMLHttpRequest&#39; } } ``` 2. **自定义上传处理**: ```js // 替换默认上传逻辑 editor.customConfig.customUploadImg = function (files, insertImgFn) { // files 是 input 中选中的文件列表 const formData = new FormData() formData.append(&#39;file&#39;, files[0]) axios.post(&#39;/api/upload&#39;, formData).then(res => { if (res.data.code === 0) { insertImgFn(res.data.url) } }) } ``` ### 五、版本差异注意点 1. **DOM操作方式**: v4.x需要手动操作DOM元素创建编辑器,而v5+使用官方Vue组件[^2] 2. **配置项差异**: v4.x使用`customConfig`对象配置,v5+改用`editorConfig`[^1] 3. **销毁方式**: v4.x需要显式调用`destroy()`,v5+通过组件生命周期自动管理[^3] ### 六、安全建议 1. **内容过滤**: ```js // 提交时过滤危险标签 const cleanHtml = this.detailContent .replace(/<script.*?>.*?<\/script>/gi, &#39;&#39;) .replace(/onerror=".*?"/gi, &#39;&#39;) ``` 2. **后端验证**: - 校验文件MIME类型 - 限制文件存储路径 - 重命名上传文件 --相关问题-- 1. 如何实现wangeditor v4.x的图片跨域上传配置? 2. 怎样在v4.x版本中自定义图片上传的请求头? 3. 如何解决wangeditor v4.x版本的内容XSS安全问题?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值