vue3+ts百度编辑器

看图

在这里插入图片描述

1、下载UEditor,放到public文件夹下,没用官方文档,找了一个大佬给的包,下载地址https://gitee.com/xxwwbb/ueditor

在这里插入图片描述

2、安装组件

npm i [email protected] -S

3、在main.ts里注册

在这里插入图片描述

4、在vue文件中使用

<template>
  <vue-ueditor-wrap
    ref="editorRef"
    v-model="msg"
    editor-id="rf-editor"
    :config="editorConfig"
    :class="isDisabled ? 'editor-disabled' : ''"
    @ready="ready"
  ></vue-ueditor-wrap>
</template>
<script setup lang="ts">
import {
    
     defineProps, defineEmits, ref, reactive, computed } from "vue";
interface Props {
    
    
  editorId?: string;
  editorConfig: any;
  isDisabled?: boolean;
}
const editorRef = ref<any>(null);
const msg = ref(""); // 编辑器内容
const isDisabled = false; // 是否只读
const editorConfig= ref({
    
    
  // 编辑器不自动被内容撑高
  autoHeightEnabled: false,
  // 初始容器高度2
  initialFrameHeight: 240,
  // 初始容器宽度
  initialFrameWidth: "100%",
  // 上传文件接口
  serverUrl: "",
  // 访问 UEditor 静态资源的根路径publc里面的名称
  UEDITOR_HOME_URL: "/UEditor/",
  enableAutoSave: true,
  toolbars: [
    [
      "undo",
      "redo",
      "|",
      "bold",
      "italic",
      "underline",
      "fontborder",
      "strikethrough",
      "superscript",
      "subscript",
      "removeformat",
      "formatmatch",
      "autotypeset",
      "blockquote",
      "pasteplain",
      "|",
      "forecolor",
      "backcolor",
      "insertorderedlist",
      "insertunorderedlist",
      "selectall",
      "cleardoc",
      "|",
      "rowspacingtop",
      "rowspacingbottom",
      "lineheight",
      "|",
      "customstyle",
      "paragraph",
      "fontfamily",
      "fontsize",
      "|",
      "directionalityltr",
      "directionalityrtl",
      "indent",
      "|",
      "justifyleft",
      "justifycenter",
      "justifyright",
      "justifyjustify",
      "|",
      "touppercase",
      "tolowercase",
      "|",
      "link",
      "unlink",
      "|",
      "imagenone",
      "imageleft",
      "imageright",
      "imagecenter",
      "|",
      "simpleupload",
      "insertimage",
      "insertvideo",
      "attachment",
      "pagebreak",
      "background",
      "|",
      "horizontal",
      "date",
      "time",
      "spechars",
      "|",
      "inserttable",
      "deletetable",
      "insertparagraphbeforetable",
      "insertrow",
      "deleterow",
      "insertcol",
      "deletecol",
      "mergecells",
      "mergeright",
      "mergedown",
      "splittocells",
      "splittorows",
      "splittocols",
      "|",
      "print",
      "preview",
      "searchreplace"
    ]
  ]
});
const ready = (editorInstance: any) => {
    
    
  if (isDisabled) {
    
    
    // 不是编辑状态,禁用编辑,隐藏工具栏
    editorInstance.body.contentEditable = false;
  }
};
// 初始化编辑器的值
const initEditor = () => {
    
    
  msg.value = ‘我是默认值’;
};
// 得到编辑器的值
const getMsg = () => {
    
    
  return msg.value;
};
</script>
<style lang="scss">
.editor-disabled {
    
    
  .edui-editor-toolbarbox {
    
    
    display: none;
  }
  .edui-editor-bottomContainer {
    
    
    display: none;
  }
}
</style>

好了,vue3使用百度编辑器就完成啦!

当编辑器只读的时候,首先在ready方法里禁用了编辑器,然后用样式隐藏了编辑器的工具条。

工具条中我只放了自己需要的功能,不是全部功能哦

完成了给个小心心吧

猜你喜欢

转载自blog.csdn.net/weixin_42255789/article/details/127772185