WangEditor在Vue前端的应用

1、在Vue项目中安装WangEditor
对于Vue2:
npm install @wangeditor/editor-for-vue --save
或者 yarn add @wangeditor/editor-for-vue
对于Vue3:
npm install @wangeditor/editor-for-vue@next --save
或者 yarn add @wangeditor/editor-for-vue@next
2、将WangEditor封装成组件WangEditor.vue

<template>
    <div>
      <div>
        <button @click="insertText">insert text</button>
        <button @click="printHtml">print html</button>
        <button @click="disable">disable</button>
      </div>
      <div style="border: 1px solid #ccc; margin-top: 10px">
        <Toolbar
          :editor="editorRef"
          :defaultConfig="toolbarConfig"
          :mode="mode"
          style="border-bottom: 1px solid #ccc"
        />
        <Editor
          :defaultConfig="editorConfig"
          :mode="mode"
          v-model="valueHtml"
          style="height: 400px; overflow-y: hidden"
          @onCreated="handleCreated"
          @onChange="handleChange" @onDestroyed="handleDestroyed" @onFocus="handleFocus" @onBlur="handleBlur" @customAlert="customAlert" @customPaste="customPaste" /> </div> <div style="margin-top: 10px"> <textarea v-model="valueHtml" readonly style="width: 100%; height: 200px; outline: none" ></textarea> </div> </div> </template> <script> import '@wangeditor/editor/dist/css/style.css';
  import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue';
  import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
  
  export default {
    components: { Editor, Toolbar },
    setup() {
      // 编辑器实例,必须用 shallowRef,重要!
      const editorRef = shallowRef();
  
      // 内容 HTML
      const valueHtml = ref('<p>hello</p>');
  
      // 模拟 ajax 异步获取内容
      onMounted(() => {
        setTimeout(() => {
          valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>';
        }, 1500);
      });
  
      const toolbarConfig = {};
      const editorConfig = { placeholder: '请输入内容...' };
  
      // 组件销毁时,也及时销毁编辑器,重要!
      onBeforeUnmount(() => {
        const editor = editorRef.value;
        if (editor == null) return;
  
        editor.destroy();
      });
  
      // 编辑器回调函数
      const handleCreated = (editor) => {
        console.log('created', editor);
        editorRef.value = editor; // 记录 editor 实例,重要!
      };
      const handleChange = (editor) => {
        console.log('change:', editor.getHtml());
      };
      const handleDestroyed = (editor) => {
        console.log('destroyed', editor);
      };
      const handleFocus = (editor) => {
        console.log('focus', editor);
      };
      const handleBlur = (editor) => {
        console.log('blur', editor);
      };
      const customAlert = (info, type) => {
        alert(`【自定义提示】${type} - ${info}`);
      };
      const customPaste = (editor, event, callback) => {
        console.log('ClipboardEvent 粘贴事件对象', event);
  
        // 自定义插入内容
        editor.insertText('xxx');
  
        // 返回值(注意,vue 事件的返回值,不能用 return)
        callback(false); // 返回 false ,阻止默认粘贴行为
        // callback(true) // 返回 true ,继续默认的粘贴行为
      };
  
      const insertText = () => {
        const editor = editorRef.value;
        if (editor == null) return;
  
        editor.insertText('hello world');
      };
  
      const printHtml = () => {
        const editor = editorRef.value;
        if (editor == null) return;
        console.log(editor.getHtml());
      };
  
      const disable = () => {
        const editor = editorRef.value;
        if (editor == null) return;
        editor.disable()
      };
  
      return {
        editorRef,
        mode: 'default',
        valueHtml,
        toolbarConfig,
        editorConfig,
        handleCreated,
        handleChange,
        handleDestroyed,
        handleFocus,
        handleBlur,
        customAlert,
        customPaste,
        insertText,
        printHtml,
        disable
      };
    },
  };
  </script>
  

3、调用组件

<template>
  <div>
    <WangEditor></WangEditor>
  </div>
</template>

<script setup>
import WangEditor from './WangEditor.vue'
</script>

4、运行效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 3中使用WangEditor实现全屏功能,你可以按照以下步骤进行操作: 1. 首先,安装WangEditorVue版本: ```shell npm install wangeditor-vue@latest --save ``` 2. 在需要使用WangEditor组件中,引入WangEditor和样式文件: ```javascript import WangEditor from 'wangeditor-vue'; import 'wangeditor/dist/css/wangEditor.min.css'; ``` 3. 在组件中注册WangEditor组件: ```javascript components: { WangEditor }, ``` 4. 在模板中使用WangEditor组件,并设置相关属性: ```html <template> <div> <wang-editor v-model="content" :config="editorConfig"></wang-editor> </div> </template> ``` 其中,`v-model`绑定了编辑器的内容,`editorConfig`是编辑器的配置项。 5. 在组件的`data`中定义`editorConfig`对象,设置全屏按钮的配置: ```javascript data() { return { content: '', // 编辑器内容 editorConfig: { menus: [ 'head', // 标题 'bold', // 粗体 'italic', // 斜体 'underline', // 下划线 'strikeThrough', // 删除线 'foreColor', // 文字颜色 'backColor', // 背景颜色 'fullscreen' // 全屏 ], fullscreen: true // 默认全屏 } }; } ``` 在`menus`中添加了`fullscreen`按钮,并设置其为默认显示。`fullscreen`为`true`表示默认全屏。 6. 最后,可以在样式中设置编辑器的宽度和高度: ```html <style scoped> .wangeditor-container { width: 100%; height: 400px; } </style> ``` 这样就完成了在Vue 3中使用WangEditor实现全屏功能的配置。你可以根据需求自定义编辑器的样式和其他配置项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值