vue3中使用富文本

注:参考:

https://www.wangeditor.com/;

1、导入一下内容:
npm install @wangeditor/editor --save
npm install @wangeditor/editor-for-vue@next --save
还有就是在package.json中添加并下载:"@tinymce/tinymce-vue": "^6.1.0",不然输入中文报错

2、在需要用到的地方使用:
标签:
 <editor v-model="ruleForm.tushuContent" @input="handleEditorInput"></editor>
引用:
import editor from "@/components/common/Editor.vue";
export default {
components: {
    editor
  },
}
对应js:
handleEditorInput(newContent) {
//其中this.ruleForm.tushuContent更换为你的字段属性
      this.ruleForm.tushuContent = newContent;
    },

3、富文本组件:
<template>
  <div>
    <div style="border: 1px solid #ccc">
      <Toolbar
          style="border-bottom: 1px solid #ccc"
          :editor="editorRef"
          :defaultConfig="toolbarConfig"
          :mode="mode"
      />
      <Editor
          style="height: 500px; overflow-y: hidden;"
          ref="editorRef"
          v-model="valueHtml"
          :defaultConfig="editorConfig"
          :mode="mode"
          @onCreated="handleCreated"
          @onChange="handleChange"
          @onDestroyed="handleDestroyed"
          @onFocus="handleFocus"
          @onBlur="handleBlur"
          @customAlert="customAlert"
          @customPaste="customPaste"
      />
    </div>
  </div>
</template>

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

export default {
  components: { Editor, Toolbar },
  props: {
    modelValue: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      editorRef: null,
      toolbarConfig: {excludeKeys: ['insertLink', 'group-image', 'group-video']},
      editorConfig: {placeholder: '请输入内容...',},
      mode: 'default', 
      valueHtml: '' 
    };
  },
  watch: {
    modelValue(newVal) {
      this.valueHtml = newVal;
    },
    valueHtml(newVal) {
      this.$emit('update:modelValue', newVal);
    }
  },
  methods: {
    handleCreated(editor) {
      this.editorRef = editor;
      if (this.modelValue) {
        editor.children.html(this.modelValue);
      }
    },
    handleChange(newHtml) {
    },
    handleDestroyed(editor) {
    },
    handleFocus(editor) {
    },
    handleBlur(editor) {
    },
    customAlert(info, type) {
      alert(`【自定义提示】${type} - ${info}`);
    },
    customPaste(editor, event, callback) {
      event.preventDefault();
      callback(false);
    }
  },
  mounted() {
    this.valueHtml = this.modelValue;
  },
  beforeDestroy() {
    if (this.editorRef) {
      this.editorRef.destroy();
    }
  }
};
</script>

注意1:给富文本的写法是vue2的写法,但部分vue3也适用,如果用不成考虑适用Ai将整个内容转化为使用vue3的写法即可.
注意2:其中链接+图片+视频会报错我给隐藏了,想用的你们可以研究一下,最上面的链接是官网

<think>嗯,用户想了解在Vue3中如何实现富文本编辑器,需要插件和示例。我之前处理过类似的问题,记得有两个主流的库,一个是@wangeditor/editor-for-vue,另一个是TinyMCE。那我要先回忆一下这两个库的具体用法。 首先,用户提到的是Vue3,所以需要确保插件是否兼容Vue3。@wangeditor/editor-for-vue是专门为Vue3设计的,而TinyMCE也有对应的Vue3版本,比如@tinymce/tinymce-vue。不过可能需要不同的配置步骤。 然后,用户需要示例代码。我需要分别整理这两个插件的安装步骤和基本组件使用。比如,对于wangeditor,可能需要先安装依赖,然后在组件中引入,使用shallowRef来管理编辑器实例,并处理数据绑定和生命周期。对于TinyMCE,可能需要安装两个包,tinymce和@tinymce/tinymce-vue,然后配置静态资源路径,尤其是在Vue项目中可能需要将某些文件复制到public目录下。 另外,需要注意两者的不同点。比如,wangeditor可能更轻量,而TinyMCE功能更全面但体积较大。异步初始化也是一个需要注意的地方,特别是TinyMCE可能需要等待脚本加载完成。 还有,用户可能关心数据绑定,比如v-model的使用,以及如何处理图片上传等自定义功能。这些都需要在示例中展示出来,比如使用自定义事件来处理图片上传,并更新编辑器内容。 最后,需要生成相关问题,帮助用户进一步探索,比如性能优化、扩展功能、安全性处理等。这些都需要基于已有的引用内容,比如提到的shallowRef和异步操作。 现在要检查步骤是否正确,确保代码示例没有错误,比如是否正确使用setup语法,是否处理了组件的挂载和销毁生命周期,以及静态资源的路径是否正确。还要确保引用的标识正确对应到参考资料,比如引用[^1]和[^2]分别对应到两个不同的库。</think>在Vue3中实现富文本编辑器主要有两种主流方案,以下是具体实现方法和示例: ### 一、基于@wangeditor/editor-for-vue的实现 1. **安装依赖** ```bash npm install @wangeditor/editor @wangeditor/editor-for-vue ``` 2. **组件实现** ```vue <template> <div style="border: 1px solid #ccc"> <Toolbar :editor="editorRef" :defaultConfig="toolbarConfig" style="border-bottom: 1px solid #ccc" /> <Editor v-model="html" :defaultConfig="editorConfig" style="height: 400px" @onCreated="handleCreated" /> </div> </template> <script setup> import { shallowRef } from &#39;vue&#39; import { Editor, Toolbar } from &#39;@wangeditor/editor-for-vue&#39; // 编辑器实例(使用shallowRef优化性能) const editorRef = shallowRef() const html = ref(&#39;<p>初始内容</p>&#39;) // 工具栏配置 const toolbarConfig = { excludeKeys: [&#39;uploadImage&#39;] } // 编辑器配置 const editorConfig = { placeholder: &#39;请输入内容...&#39;, MENU_CONF: { uploadImage: { async customUpload(file, insertFn) { // 自定义图片上传逻辑 const url = await uploadToServer(file) insertFn(url) } } } } const handleCreated = (editor) => { editorRef.value = editor } // 组件销毁时销毁编辑器 onBeforeUnmount(() => { const editor = editorRef.value if (editor) editor.destroy() }) </script> ``` 该方案通过shallowRef管理编辑器实例,利用v-model实现双向绑定,并通过自定义上传函数处理图片等多媒体内容[^1]。 ### 二、基于TinyMCE的实现 1. **安装依赖** ```bash npm install tinymce @tinymce/tinymce-vue ``` 2. **静态资源配置** 将node_modules/tinymce下的icons、plugins、skins、themes目录复制到public/tinymce 3. **组件实现** ```vue <template> <Editor v-model="content" :init="initOptions" api-key="your-api-key" /> </template> <script setup> import Editor from &#39;@tinymce/tinymce-vue&#39; const content = ref(&#39;<p>初始内容</p>&#39;) const initOptions = { height: 500, plugins: &#39;image media link lists&#39;, toolbar: &#39;bold italic | image media&#39;, images_upload_handler: async (blobInfo) => { const url = await uploadToServer(blobInfo.blob()) return url }, content_css: &#39;/tinymce/skins/content/default/content.css&#39; } </script> ``` 该方案需要配置静态资源路径,通过官方提供的Vue组件封装实现富文本功能。 ### 三、方案对比 | 特性 | wangeditor | TinyMCE | |----------------|------------|----------| | 包体积 | 约1MB | 约5MB | | 中文支持 | 原生支持 | 需要配置 | | 图片上传 | 自定义实现 | 内置支持 | | 移动端适配 | 优秀 | 良好 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值