私人订制潤路线 發表於 2025-7-27 20:07:00

在Vue3+ElementPlus前端中增加对@wangeditor的富文本编辑器和上传文件的处理的封装,实现系统新闻资讯的管理

<p>在很多业务系统中,有时候需要编辑富文本,就是包括图文消息排版等处理,图片有时候需要结合后端实现图片的上传处理,在本篇随笔中,结合@wangeditor的富文本编辑器和上传文件的处理的封装,来实现系统新闻资讯的管理,可以实现图片上传和图文排版的常规操作。</p>
<h3>1、@wangeditor的富文本编辑器</h3>
<p>该富文本编辑器控件的官网地址:https://www.wangeditor.com/,官网提供很详尽的使用介绍。</p>
<p>对应Vue版本的编辑器控件是:<code>@wangeditor/editor-for-vue</code></p>
<p>因此我们可以通过pnpm 、npm等安装管理工具进行组件的安装。</p>
<div class="cnblogs_code">
<pre>pnpm <span style="color: rgba(0, 0, 255, 1)">install</span> @wangeditor/editor-<span style="color: rgba(0, 0, 255, 1)">for</span>-vue --save</pre>
</div>
<p>其他的处理就和常规的组件使用差不多了。我们来大概介绍下使用的案例。</p>
<p>界面代码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">="border: 1px solid #ccc;"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">Toolbar
            </span><span style="color: rgba(255, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">="border-bottom: 1px solid #ccc"</span><span style="color: rgba(255, 0, 0, 1)">
            :editor</span><span style="color: rgba(0, 0, 255, 1)">="editor"</span><span style="color: rgba(255, 0, 0, 1)">
            :defaultConfig</span><span style="color: rgba(0, 0, 255, 1)">="toolbarConfig"</span><span style="color: rgba(255, 0, 0, 1)">
            :mode</span><span style="color: rgba(0, 0, 255, 1)">="mode"</span>
      <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">Editor
            </span><span style="color: rgba(255, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">="height: 500px; overflow-y: hidden;"</span><span style="color: rgba(255, 0, 0, 1)">
            v-model</span><span style="color: rgba(0, 0, 255, 1)">="html"</span><span style="color: rgba(255, 0, 0, 1)">
            :defaultConfig</span><span style="color: rgba(0, 0, 255, 1)">="editorConfig"</span><span style="color: rgba(255, 0, 0, 1)">
            :mode</span><span style="color: rgba(0, 0, 255, 1)">="mode"</span><span style="color: rgba(255, 0, 0, 1)">
            @onCreated</span><span style="color: rgba(0, 0, 255, 1)">="onCreated"</span>
      <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">template</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>脚本控制代码</p>
<div class="cnblogs_code">
<pre>&lt;script&gt;<span style="color: rgba(0, 0, 0, 1)">
import Vue from </span>'vue'<span style="color: rgba(0, 0, 0, 1)">
import { Editor, Toolbar } from </span>'@wangeditor/editor-for-vue'<span style="color: rgba(0, 0, 0, 1)">

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> Vue.extend({
    components: { Editor, Toolbar },
    data() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
      editor: </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,
      html: </span>'&lt;p&gt;hello&lt;/p&gt;'<span style="color: rgba(0, 0, 0, 1)">,
      toolbarConfig: {},
      editorConfig: { placeholder: </span>'请输入内容...'<span style="color: rgba(0, 0, 0, 1)"> },
      mode: </span>'default', <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> or 'simple'</span>
<span style="color: rgba(0, 0, 0, 1)">      }
    },
    methods: {
      onCreated(editor) {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.editor = Object.seal(editor) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 一定要用 Object.seal() ,否则会报错</span>
<span style="color: rgba(0, 0, 0, 1)">      },
    },
    mounted() {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 模拟 ajax 请求,异步渲染编辑器</span>
      setTimeout(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.html = '&lt;p&gt;模拟 Ajax 异步设置内容 HTML&lt;/p&gt;'<span style="color: rgba(0, 0, 0, 1)">
      }, </span>1500<span style="color: rgba(0, 0, 0, 1)">)
    },
    beforeDestroy() {
      const editor </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.editor
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (editor == <span style="color: rgba(0, 0, 255, 1)">null</span>) <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
      editor.destroy() </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件销毁时,及时销毁编辑器</span>
<span style="color: rgba(0, 0, 0, 1)">    },
})
</span>&lt;/script&gt;</pre>
</div>
<p>通过官方案例的指引,我们可以对其工具栏、编辑器配置、菜单、以及相应API进行处理即可。</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727174040839-124979869.png" alt="image" width="172" height="168" loading="lazy"></p>
<p>界面效果如下所示。</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727174217629-1102806564.png" alt="image" width="677" height="222" loading="lazy"></p>
<p>&nbsp;</p>
<h3>2、自定义上传文件的处理</h3>
<p>一般富文本的编辑器,都会提供相应的图片上传接口处理,该@wangeditor的富文本编辑器也是提供了很弹性化的文件上传接口的处理,以便兼容我们各自五花八门的图片上传接口处理。</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727175036018-1098668473.png" alt="image" width="968" height="488" loading="lazy"></p>
<p>通过官网的配置介绍,我们可以适应自己后端的接口和相关的配置信息。</p>
<p>如我的后端上传文件的接口为:&nbsp;/api/fileupload/postupload ,后端接口定义如下所示。</p>
<div class="cnblogs_code">
<pre>      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 多文件上传处理(自动根据配置文件选择合适的上传方式)
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
       <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">请求正文最大大小100M</span>
<span style="color: rgba(0, 0, 0, 1)">      
      
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">async</span> Task&lt;List&lt;ResponseFileInfo&gt;&gt;<span style="color: rgba(0, 0, 0, 1)"> PostUpload()
      {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> httpContext = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.HttpContext;
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> form = <span style="color: rgba(0, 0, 255, 1)">await</span><span style="color: rgba(0, 0, 0, 1)"> Request.ReadFormAsync();
            </span><span style="color: rgba(0, 0, 255, 1)">string</span>? guid = form[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">guid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];
            </span><span style="color: rgba(0, 0, 255, 1)">string</span>? folder = form[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">folder</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> files = form.Files;</pre>
</div>
<p>其中我们接受的参数包括guid(附件的GUID),folder(图片类别)以及接收Files的集合。返回的集合里面包括有id,文件名称,文件地址等内容,如下所示。</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727180308117-539178832.png" alt="image" width="593" height="657" loading="lazy"></p>
<p>因此根据我的后端接口上传处理,传入相关的参数来处理编辑器控件配置。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 更多详细配置看 https://www.wangeditor.com/v5/menu-config.html#%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87</span>
editorConfig.MENU_CONF["uploadImage"] =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 服务端上传地址,根据实际业务改写</span>
server: "/api/fileupload/postupload"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> form-data 的 fieldName,根据实际业务改写</span>
fieldName: "file"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 选择文件时的类型限制,根据实际业务改写</span>
allowedFileTypes: ["image/png", "image/jpg", "image/jpeg"<span style="color: rgba(0, 0, 0, 1)">],

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 单个文件的最大体积限制,默认为 2M</span>
maxFileSize: 1 * 1024 * 1024, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1M</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 最多可上传几个文件,默认为 100</span>
maxNumberOfFiles: 10<span style="color: rgba(0, 0, 0, 1)">,

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义上传参数,参数会被添加到 formData 中,一起上传到服务端。</span>
<span style="color: rgba(0, 0, 0, 1)">meta: {
    folder: props.imageFolder,
    guid: props.imageGuid,
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义增加 httpheaders,如果接口需要令牌头信息,需要带上</span>
<span style="color: rgba(0, 0, 0, 1)">headers: {
    Accept: </span>'text/x-json'<span style="color: rgba(0, 0, 0, 1)">,
    Authorization: </span>'Bearer ' +<span style="color: rgba(0, 0, 0, 1)"> getAccessToken(),
},

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将 meta 拼接到 url 参数中,默认 false</span>
metaWithUrl: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义插入图片</span>
<span style="color: rgba(0, 0, 0, 1)">customInsert(res: any, insertFn: InsertFnType) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log("customInsert", res);</span>
    const result = res.result as Array&lt;ResponseFileInfo&gt;<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (result.length &gt; 0<span style="color: rgba(0, 0, 0, 1)">) {
      const { id, name, url } </span>= result;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> res.data.url是后端返回的图片地址,根据实际业务改写</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (url) {
      setTimeout(() </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {

          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> insertFn插入图片进编辑器</span>
<span style="color: rgba(0, 0, 0, 1)">          insertFn(url, name, url);
      }, </span>2000<span style="color: rgba(0, 0, 0, 1)">);
      }
    }
}
};</span></pre>
</div>
<p>根据返回的结果,我们调用编辑器的接口实现图片的插入。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义插入图片</span>
<span style="color: rgba(0, 0, 0, 1)">customInsert(res: any, insertFn: InsertFnType) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log("customInsert", res);</span>
    const result = res.result as Array&lt;ResponseFileInfo&gt;<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (result.length &gt; 0<span style="color: rgba(0, 0, 0, 1)">) {
      const { id, name, url } </span>= result;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> res.data.url是后端返回的图片地址,根据实际业务改写</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (url) {
      setTimeout(() </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {

          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> insertFn插入图片进编辑器</span>
<span style="color: rgba(0, 0, 0, 1)">          insertFn(url, name, url);
      }, </span>2000<span style="color: rgba(0, 0, 0, 1)">);
      }
    }
}
};</span></pre>
</div>
<p>&nbsp;</p>
<h3>3、实现框架中的系统新闻资讯的管理</h3>
<p>为了方便使用富文本编辑器,不用每次都配置一次相关的参数,我们把集成好上传图片的功能整合到自定义编辑器控件里面去,因此就可以实现重用了。</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727180923947-1263590439.png" alt="image" width="303" height="448" loading="lazy"></p>
<p>&nbsp;因此定义了该控件的几个可以传入来的相关属性参数如下所示。</p>
<div class="cnblogs_code">
<pre>const props =<span style="color: rgba(0, 0, 0, 1)"> defineProps({
modelValue: {
    type: String,
    required: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
},
placeholder: {
    type: String,
    </span><span style="color: rgba(0, 0, 255, 1)">default</span>: '请输入内容...'<span style="color: rgba(0, 0, 0, 1)">,
    required: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
},
imageFolder: {
    type: String,
    </span><span style="color: rgba(0, 0, 255, 1)">default</span>: '文章图片'<span style="color: rgba(0, 0, 0, 1)">,
    required: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
},
imageGuid: {
    type: String,
    </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">: util.guid(),
    required: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
}
});</span></pre>
</div>
<p>并在内容变化的时候,触发事件通知。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Emits</span>
const emit = defineEmits&lt;<span style="color: rgba(0, 0, 0, 1)">{
(e: </span>'update:modelValue', val: any | any[]): <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)">;
}</span>&gt;();</pre>
</div>
<p>再把前面的编辑器自定义参数处理内容整合进去。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 更多详细配置看 https://www.wangeditor.com/v5/menu-config.html#%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87</span>
editorConfig.MENU_CONF["uploadImage"] =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 服务端上传地址,根据实际业务改写</span>
server: "/api/fileupload/postupload"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> form-data 的 fieldName,根据实际业务改写</span>
fieldName: "file"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 选择文件时的类型限制,根据实际业务改写</span>
allowedFileTypes: ["image/png", "image/jpg", "image/jpeg"<span style="color: rgba(0, 0, 0, 1)">],

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 单个文件的最大体积限制,默认为 2M</span>
maxFileSize: 1 * 1024 * 1024, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 1M</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 最多可上传几个文件,默认为 100</span>
maxNumberOfFiles: 10<span style="color: rgba(0, 0, 0, 1)">,

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义上传参数,参数会被添加到 formData 中,一起上传到服务端。</span>
<span style="color: rgba(0, 0, 0, 1)">meta: {
    folder: props.imageFolder,
    guid: props.imageGuid,
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义增加 httpheaders,如果接口需要令牌头信息,需要带上</span>
<span style="color: rgba(0, 0, 0, 1)">headers: {
    Accept: </span>'text/x-json'<span style="color: rgba(0, 0, 0, 1)">,
    Authorization: </span>'Bearer ' +<span style="color: rgba(0, 0, 0, 1)"> getAccessToken(),
},

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将 meta 拼接到 url 参数中,默认 false</span>
metaWithUrl: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">,

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义插入图片</span>
<span style="color: rgba(0, 0, 0, 1)">customInsert(res: any, insertFn: InsertFnType) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log("customInsert", res);</span>
    const result = res.result as Array&lt;ResponseFileInfo&gt;<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (result.length &gt; 0<span style="color: rgba(0, 0, 0, 1)">) {
      const { id, name, url } </span>= result;
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> res.data.url是后端返回的图片地址,根据实际业务改写</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (url) {
      setTimeout(() </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {

          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> insertFn插入图片进编辑器</span>
<span style="color: rgba(0, 0, 0, 1)">          insertFn(url, name, url);
      }, </span>2000<span style="color: rgba(0, 0, 0, 1)">);
      }
    }
}
};</span></pre>
</div>
<p>由于我们后端的接口不是匿名的,需要令牌,因此需要额外增加对应的Token令牌信息的头部。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义增加 httpheaders,如果接口需要令牌头信息,需要带上</span>
<span style="color: rgba(0, 0, 0, 1)">headers: {
    Accept: </span>'text/x-json'<span style="color: rgba(0, 0, 0, 1)">,
    Authorization: </span>'Bearer ' +<span style="color: rgba(0, 0, 0, 1)"> getAccessToken(),
},</span></pre>
</div>
<p>然后监控相关的内容处理即可。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 组件销毁时,也及时销毁编辑器</span>
onBeforeUnmount(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
const editor </span>=<span style="color: rgba(0, 0, 0, 1)"> editorRef.value;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (editor == <span style="color: rgba(0, 0, 255, 1)">null</span>) <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
editor.destroy();
});

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载的时候,赋值给编辑器</span>
onMounted(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
valueHtml.value </span>= props.modelValue ?? ""<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(props.modelValue)</span>
<span style="color: rgba(0, 0, 0, 1)">});

watch(() </span>=&gt; props.modelValue, (val) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
valueHtml.value </span>=<span style="color: rgba(0, 0, 0, 1)"> val
})

watch(() </span>=&gt; valueHtml.value, (newVal) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
emit(</span>"update:modelValue"<span style="color: rgba(0, 0, 0, 1)">, newVal)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 使用 nextTick 确保 DOM 更新完成后再更新编辑器内容</span>
nextTick(() =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (editorRef.value) {
      editorRef.value.setHtml(newVal);
    }
});
})</span></pre>
</div>
<p>完成编辑器的自定义控件,如下所示。</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727181443669-858799013.png" alt="image" width="1045" height="793" loading="lazy"></p>
<p>&nbsp;然后我们在具体的页面里面,导入自定义控件,就可以在页面代码中使用自己的富文本编辑控件了。</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">my-editor </span><span style="color: rgba(255, 0, 0, 1)">v-model</span><span style="color: rgba(0, 0, 255, 1)">="editForm.content"</span><span style="color: rgba(255, 0, 0, 1)"> image-folder</span><span style="color: rgba(0, 0, 255, 1)">="文章图片"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span></pre>
</div>
<p>在我们完成了@wangeditor的富文本编辑器和上传文件的处理的封装后,我们可以用它来实现系统新闻资讯的管理。</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727183930599-326311473.png" alt="image" width="966" height="711" loading="lazy"></p>
<p>&nbsp;然后尝试上传文件处理</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727184046475-1884558272.png" alt="image" width="972" height="360" loading="lazy"></p>
<p>测试上传文件,完成后端接口对接处理即可。</p>
<p>这样我们就可以在多端中查看或者处理相关的系统新闻资讯内容了,如以下界面是通过H5对接后端接口,实现新闻资讯内容的管理。&nbsp;</p>
<p><img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727184420692-1794088578.png" alt="image" width="350" height="747" loading="lazy">&nbsp; &nbsp;<img src="https://img2024.cnblogs.com/blog/8867/202507/8867-20250727184506462-553876639.png" alt="image" width="349" height="745" loading="lazy"></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <div style="border-right-color: #cccccc; border-right-width: 1px; border-right-style: solid; padding-right: 5px; border-top-color: #cccccc; border-top-width: 1px; border-top-style: solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; width: 98%; padding-top: 4px; border-bottom-color: #cccccc; border-bottom-width: 1px; border-bottom-style: solid; background-color: #eeeeee;">
    <img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt>
    <span style="color: #000000"><span class="Apple-tab-span" style="white-space: pre"></span>
   专注于代码生成工具、.Net/Python 框架架构及软件开发,以及各种Vue.js的前端技术应用。著有Winform开发框架/混合式开发框架、微信开发框架、Bootstrap开发框架、ABP开发框架、SqlSugar开发框架、Python开发框架等框架产品。
   <br>  转载请注明出处:撰写人:伍华聪  http://www.iqidi.com <br>    </span></div><br><br>
来源:https://www.cnblogs.com/wuhuacong/p/19007657
頁: [1]
查看完整版本: 在Vue3+ElementPlus前端中增加对@wangeditor的富文本编辑器和上传文件的处理的封装,实现系统新闻资讯的管理