vue中使用富文本编辑器,@tinymce/tinymce-vue

富文本就是在后台管理系统中常见的录入带格式的内容,如:表格,字体加粗,斜体,文字颜色等等,就像一个word一样。类似于这样的效果:

我们使用通用在线编辑器tinymce。支持vue和react。

1. 安装

npm i @tinymce/tinymce-vue -S

要注意版本,我使用的是5.1.1。

2. 在vue组件里导入

import Editor from '@tinymce/tinymce-vue';

如果是选项式api的话,需要做组件注册。

3. 在模板上渲染

<Editor ref="editorref" :init="{plugins: 'wordcount'}" />

4. 操作富文本实例

editorref.value.getEditor()

5、获取内容【这是最关键的一步】

editorref.value.getEditor().getContent()

完整代码【使用element-plus和ts】:

复制代码
<!-- 富文本 -->
<script lang="ts" setup>
import { ref } from "vue";
import Editor from '@tinymce/tinymce-vue'


const content = ref();

interface INews {
  title: string
}
const form = ref<INews>({
  title: ""
});
const editorRef = ref();

const addNews = () => {
  let data = {
    title: form.value.title,
    content: editorRef.value.getEditor().getContent()
  }

  console.log("data",data);
  content.value = data.content;
}


</script>

<template>
  <h1>富文本</h1>
  <el-form :model="form">
    <el-form-item label="标题">
      <el-input v-model="form.title" autocomplete="off" />
    </el-form-item>
    <editor ref="editorRef" :init="{
      height: 500,
      menubar: true,
      plugins: [
        'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'preview',
        'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen',
        'insertdatetime', 'media', 'table', 'code', 'help', 'wordcount'
      ],
      toolbar: 'undo redo | blocks | ' +
        'bold italic forecolor | alignleft aligncenter ' +
        'alignright alignjustify | bullist numlist outdent indent | ' +
        'removeformat | help | image | table',
      content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }'
    }" />

    <el-form-item>
      <el-button @click="addNews">添加</el-button>
    </el-form-item>
  </el-form>

  <hr/>
  <div v-html="content"></div>
</template>

<style lang="scss" scoped></style>
相关推荐
suaizai_22 分钟前
AI Agent如何懂你:四层能力拆解
java·前端·人工智能
invicinble33 分钟前
把握前端项目的核心(vibecoding)
前端
0end11 小时前
AI Agent 学习笔记(三):上下文工程(下)—— KV Cache、提示词设计与 Agent Skills
前端·aigc·ai编程
CAD老兵1 小时前
在浏览器里对比 DWG/DXF 图纸 —— @mlightcad/cad-diff-viewer
前端·javascript·github
JasonYin1 小时前
AI 定义的 H5移动端 开发规范,直接抄作业!
前端
诗章与猫1 小时前
Leaflet 渲染 100 万 marker 卡成 PPT?我用 WebGL 重写了渲染器
前端
用户921080262861 小时前
在 AI 代码生成项目里接入 Thought:别展示“玄学思维链”,只展示用户真正关心的工具调用
前端
WL_arm2 小时前
Vibe Coding(氛围编程)入门
javascript·css·人工智能·html5
艾醒(AiXing-w)2 小时前
LangChain 1.0 入门(二):LangChain 全模型标准化接入最佳实践(小白参数详解版)
前端·javascript·langchain
计算机魔术师2 小时前
我看了 Hugging Face 的 Daily Papers,发现这件事
前端