You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vue/Nuxt的Tiptap编辑器中添加Base64图片?

在Vue/Nuxt中实现Tiptap编辑器Base64图片上传功能异常问题

我在Vue和Nuxt.js中使用Tiptap编辑器,想要实现用户上传图片的功能。常规方式是通过弹窗让用户输入图片URL来加载显示,但我想通过input标签,在change事件触发时获取图片的Base64格式URL并插入编辑器中,不过目前该功能无法正常工作。

以下是我的实现代码:

Data 部分代码

data() {
  return {
    editor: new Editor({
      content: '<p>Write Something.....</p>',
      extensions: [
        StarterKit,
        Image.configure({
          allowBase64: true,
          inline: true,
        }),
      ],
    }),
  };
},

模板部分代码

<label for="img-uploader" id="uploader">Upload</label>
<input
  id="img-uploader"
  type="file"
  accept="image/jpeg"
  style="display: none"
  @change="addImage"
/>

方法部分代码

addImage(event) {
  const file = event.target.files[0];

  if (file) {
    const reader = new FileReader();

    reader.onload = (e) => {
      const dataURL = e.target.result;
      console.log(dataURL);
      this.editor.chain().focus().setImage({ src: dataURL }).run();
    };

    reader.readAsDataURL(file);
  }
},

内容的提问来源于stack exchange,提问作者Dilip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 09:05:03