WangEditor在Vue前端的应用

这篇具有很好参考价值的文章主要介绍了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、运行效果
WangEditor在Vue前端的应用,Vue Web,前端,vue.js,WangEditor文章来源地址https://www.toymoban.com/news/detail-702290.html

到了这里,关于WangEditor在Vue前端的应用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【三十天精通Vue 3】第二十七天 如何用Vue 3和TensorFlow.js实现人脸识别Web应用?

    ✅创作者:陈书予 🎉个人主页:陈书予的个人主页 🍁陈书予的个人社区,欢迎你的加入: 陈书予的社区 🌟专栏地址: 三十天精通 Vue 3

    2024年02月03日
    浏览(30)
  • Vue.js 与 ViewDesign:为企业级 Web 应用提供高效可靠的解决方案

    在当今瞬息万变的商业环境中,企业需要高效、稳定且易于维护的 Web 应用程序来支持其日常运营和业务发展。幸运的是,Vue.js 和 ViewDesign 的强大组合为开发人员提供了构建复杂企业级 Web 应用程序的完美解决方案。 Vue.js 是一个开源的渐进式 JavaScript 框架,专为构建用户界面而生

    2024年03月11日
    浏览(26)
  • 发现一个开源的Chatgpt-web应用,前端使用vue编写,后端也是nodejs代码编写的。

    演示视频: https://www.bilibili.com/video/BV1WC4y1k7j5/?vd_source=4b290247452adda4e56d84b659b0c8a2 【chatglm3】(10):使用fastchat本地部署chatlgm3-6b模型,并配合chatgpt-web的漂亮界面做展示,调用成功,vue的开源项目 部署脚本和方法: https://gitee.com/fly-llm/dify-mysql-llm/tree/master/fastchat-docker-compose ht

    2024年02月11日
    浏览(29)
  • 前端Vue项目调用页面web3.js:连接metaMask钱包,(查询钱包ETH余额,查询代币余额,ETH转账,代币转账,代币授权,查询授权数量,计算价格)等功能

    这里分享下相关文档 1.web3.js中文文档 https://learnblockchain.cn/docs/web3.js/getting-started.html 2.metamask官方文档:https://docs.metamask.io/ 第一种方法:连接钱包 源码: 第二种方法: 连接钱包 安装依赖直接可以使用 源码: 其他功能: 1、在 mounted 中自动检测浏览器是否安装MetaMask钱包 2、查询

    2024年02月11日
    浏览(38)
  • WEB前端3D变换效果以及如何应用js代码

    变换效果3d 过渡属性 动画 首先JavaScript是一个 动态的解释型 编程语言。 所谓动态就是指,代码在运行时变量的数据类型可以发生变化。(静态就想类似c、java、go这种语言他们需要创建变量时就确定变量的数据类型,并且不可随意更改) 所谓的解释型语言,是指代码运行时

    2024年01月23日
    浏览(32)
  • vue封装wangEditor

    components下面创建WangEditor.vue main.js中注册这个全局组件 通过这种方式,可以在每个页面上使用组件,而无需在每个页面中重复导入和注册\\\"@wangeditor/editor-for-vue\\\"组件。记得确保已经安装好\\\"@wangeditor/editor-for-vue\\\"依赖。 组件之间可以通过props和事件来进行参数传递 使用 ↓

    2024年02月09日
    浏览(23)
  • vue使用wangEditor

    导入组件 引用 npm run dev时发现报了如下错误 原因是css语言没有安装依赖 我用的是sass( 重新启动,再次报错 Node Sass 7.0.1 版与 ^4.0.0 不兼容; 解决方案: 1、npm uninstall node-sass; 2、npm i -D sass; 3、npm run dev; 项目正常启动。 补充如果Vue wangEditor报错 Cannot set property ‘uploadImgSho

    2024年02月16日
    浏览(22)
  • Vue3使用wangEditor

    那么这一节我们在编辑公司信息的弹窗中使用富文本插件wangEditor官网 案例内效果: npm install @wangeditor/editor --save npm install @wangeditor/editor-for-vue@next --save 文档部分: 代码部分: 后端 需要注意的是,在35集《完成轮播图以及公司介绍接口》中 我们的参数是 set_value ,类型是 v

    2024年02月13日
    浏览(24)
  • vue--使用wangEditor富文本

    wangEditor是一个基于jQuery的简单、开源的富文本编辑器,而Vue.js则是一种流行的JavaScript框架。将wangEditor与Vue.js结合使用,可以方便地在Vue应用中实现富文本编辑功能。本文将介绍如何在Vue中使用wangEditor,包括安装和配置wangEditor、在Vue组件中使用wangEditor以及相关的代码说明。

    2024年02月13日
    浏览(20)
  • 前端(四)——vue.js、vue、vue2、vue3

    😊博主:小猫娃来啦 😊文章核心: vue.js、vue、vue2、vue3从全局到局部 Vue.js是一款流行的JavaScript框架 vue,vue2,vue3都是vue.js的不同版本。 Vue:Vue.js的第一个版本,也称为Vue 1.x。它于2014年首次发布,并获得了广泛的应用和认可。 Vue2:Vue.js的第二个版本,也称为Vue 2.x。它在Vu

    2024年02月12日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包