vue3封装wangEditor插件

一、概要

平常在开发过程中,无论是什么后台系统还是公司内部的系统也好,都是少不了需要用到富文本框。一个好的富文本框能更好的编辑排版我们的想要内容,但今天主要讲的是wangEditor这个富文本框插件 wangEditor

  1. 插件安装
  2. 如何在vue3中使用wangEditor插件并封装wangEditor组件
  3. wangEditor的工具配置与API配置

二、插件安装 

  1. npm安装
  2. CDN引入 

1.1、 npm安装

yarn add @wangeditor/editor-for-vue@next
# 或者 npm install @wangeditor/editor-for-vue@next --save

2.1、CDN引入

<!-- 引入 css -->
<link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">

<!-- 引入 js -->
<script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
<script>
    var E = window.wangEditor; // 全局变量
</script>

如果上述 CDN 访问不成功,可以在 npm 安装完成之后,在安装包找到 JS CSS 文件,步骤如下:

  • 新建一个 test1 文件夹,打开控制台,目录定位到该文件夹,执行 npm install @wangeditor/editor 或 yarn add @wangeditor/editor
  • 安装完成,打开 node_modules/@wangeditor/editor/dist 文件夹,即可找到 JS CSS 文件:
    • index.js
    • css/style.css
  • 把这俩文件拷贝出来,然后删掉 test1 文件夹

三、封装wangEditor组件 

  1. 组件封装
  2. 使用组件

1.  wangEditor组件封装

<template>
  <div style="border: 1px solid #ccc;">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editorRef"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="height: 700px; overflow-y: hidden;"
      v-model="html"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
      @custom-paste="customPaste"
    />
  </div>
</template>

<script lang="ts" setup>
import '@wangeditor/editor/dist/css/style.css';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import { reactive, onMounted, watch, onBeforeUnmount, shallowRef, ref } from 'vue';
interface Props {
  value: string;
}
const editorProps = defineProps<Props>();
const emit = defineEmits(['update:Html']);
// 内容HTML
const html = ref();

// 工具栏选项
const toolbarConfig = {
  excludeKeys: [
    'fullScreen',
	'code',
	'group-video',
	'codeBlock',
	'bulletedList',
	'numberedList',
	'blockquote',
	'bold',
	'italic',
	'todo',
	'insertImage',
	'insertLink',
	'emotion',
	'insertTable',
  ] // 此处不需要的工具栏选项
};

// 编辑器配置
const editorConfig = {
  placeholder: '请输入内容',
  MENU_CONF: {
    uploadImage: {
      // 自定义上传图片的接口地址
      server: import.meta.env.VITE_API_URL + "/api/.../...",
      
      // 自定义设置单个文件的最大体积限制,默认为 2M
      maxFileSize: 1 * 1024 * 1024, // 1M

      // 自定义设置最多可上传几个文件,默认为 100
      maxNumberOfFiles: 10,
      
      // 自定义设置选择文件时的类型限制,默认为 ['image/*'] 。如不想限制,则设置为 []
      allowedFileTypes: ['image/*'],

      // 自定义设置上传参数,例如传递验证的 token 等。参数会被添加到 formData 中,一起上传到服务端。
      meta: {
        token: 'xxx',
        otherKey: 'yyy'
      },

      // 自定义设置将 meta 拼接到 url 参数中,默认 false
      metaWithUrl: false,

      // 自定义设置增加 http  header
      headers: {
        Accept: 'text/x-json',
        authorization: `Bearer ` + Session.get('token'),
      },

      // 自定义设置跨域是否传递 cookie ,默认为 false
      withCredentials: true,
     
      // 自定义设置超时时间,默认为 10 秒
      timeout: 5 * 1000, // 5 秒
    }
  }
  hoverbarKeys: {
    image: {
	  // 清空 image 元素的 hoverbar
	  menuKeys: [],
	},
  },
};

// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef();

// 监听内容变化
watch(
  () => editorProps.value,
  (newVal: any) => {
     html.value = newVal;
  }
);
watch(
  () => html.value,
  (newVal: any) => {
     emit('update:HTML', newVal);
  }
);

// 组件销毁时,也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return
  editor.destroy();
});
</script>

 2. 需要使用wangEditor的地方导入使用组件

<template>
  <div class="container">
    <wangEditor :value="content" @update:HTML="editorContent" />
  </div>
</template>

<script lang="ts" setup>
import wangEditor from '/@/components/wangeditor/index.vue'; // 导入wangEditor组件
import { ref, onMounted } from 'vue';
import { api } from '/@/api/index.ts';  // 导入API接口

const content = ref();

// 获取内容数据
const getData = () => {
  api().then((res: any) => {
    if (res.code == 200) {
      content.value = res.data.content;
    };
  });
};

// 内容编辑
editorContent((val: any) => {
  content.value = val;
});

// 页面挂载时
onMounted(() => {
  getData();
});
</script>

四、 wangEditor的工具配置与API配置

相关的工具配置与API配置可以自行查阅 工具栏配置 | wangEditor  文档

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您介绍一下使用Vue3封装WangEditor富文本组件的步骤。 首先,我们需要安装WangEditorVue3,可以通过以下命令进行安装: ```bash npm install wangeditor@latest npm install vue@next ``` 接着,我们可以在Vue3的组件中使用WangEditor。下面是一个简单的示例: ```vue <template> <div ref="editorElem"></div> </template> <script> import WangEditor from 'wangeditor' export default { mounted() { const editor = new WangEditor(this.$refs.editorElem) editor.create() } } </script> ``` 在上面的代码中,我们通过import导入了WangEditor,并在mounted钩子函数中创建了一个新的编辑器实例。注意,我们需要在组件的模板中添加一个ref属性,用于引用编辑器的DOM元素。 如果您需要进一步封装WangEditor组件,可以考虑将其封装为一个Vue组件,以便在其他地方重复使用。下面是一个简单的示例: ```vue <template> <div :id="editorId"></div> </template> <script> import WangEditor from 'wangeditor' export default { props: { value: String, placeholder: String, height: { type: String, default: '300px' } }, data() { return { editorId: `editor-${Math.random().toString(36).substr(2, 9)}`, editor: null } }, mounted() { this.editor = new WangEditor(`#${this.editorId}`) this.editor.config.height = this.height this.editor.config.placeholder = this.placeholder this.editor.config.onchange = this.handleChange this.editor.create() this.editor.txt.html(this.value) }, methods: { handleChange() { this.$emit('input', this.editor.txt.html()) } }, beforeUnmount() { this.editor.destroy() } } </script> ``` 在上面的代码中,我们定义了一个WangEditor组件,并通过props接收了一些参数,包括组件的初始值、占位符和高度等。在mounted钩子函数中,我们创建了一个新的编辑器实例,并通过config属性设置了一些编辑器的配置项,包括高度、占位符和内容变化时的回调函数等。我们还通过handleChange方法监听了编辑器内容的变化,并通过$emit方法向父组件发送了一个input事件,以便在父组件中更新组件的绑定值。最后,我们在beforeUnmount钩子函数中销毁了编辑器实例,以避免内存泄漏。 使用时,您可以像使用其他自定义组件一样,在Vue3的模板中引用WangEditor组件,并通过v-model指令绑定组件的值: ```vue <template> <div> <wangeditor v-model="content" placeholder="请输入内容" height="500px" /> <div>{{ content }}</div> </div> </template> <script> import WangEditor from '@/components/WangEditor.vue' export default { components: { WangEditor }, data() { return { content: '' } } } </script> ``` 在上面的代码中,我们通过import导入了WangEditor组件,并在模板中引用了该组件。我们还通过v-model指令绑定了组件的值,以便在父组件中获取和更新该值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值