首页 > 其他分享 >Vue3 富文本编辑器

Vue3 富文本编辑器

时间:2022-12-28 17:03:11浏览次数:62  
标签:文本编辑 editorRef const valueHtml wangeditor editor Vue3 import

官网 https://www.wangeditor.com/v5/for-frame.html#%E4%BD%BF%E7%94%A8-1

 

安装:

npm install @wangeditor/editor --save

npm install @wangeditor/editor-for-vue@next --save

示例代码:

<template>
  <div style="border: 1px solid #ccc">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editorRef"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="height: 500px; overflow-y: hidden;"
      v-model="valueHtml"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
    />
  </div>
</template>
<script>
import '@wangeditor/editor/dist/css/style.css' // 引入 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) => {
      editorRef.value = editor // 记录 editor 实例,重要!
    }

    return {
      editorRef,
      valueHtml,
      mode: 'default', // 或 'simple'
      toolbarConfig,
      editorConfig,
      handleCreated
    };
  }
}
</script>    

 

标签:文本编辑,editorRef,const,valueHtml,wangeditor,editor,Vue3,import
From: https://www.cnblogs.com/zhang-hong/p/17010513.html

相关文章

  • vue3使用sweetalert2替代默认的alert/confirm框
    安装#运行时依赖package.json的dependenciesnpminstallsweetalert2--save#开发时依赖package.json的devDependenciesnpminstallsweetalert2--save-dev ......
  • vue3使用bootstrap的简单加载遮罩层
    之前有使用过bootstrap做过一个简单的加载遮罩层,现把它加入到vue中。加载遮罩层一般来讲整个app共用一个就可以,因此放到App.vue中,为不影响其它的业务逻辑,放到</template>......
  • vue3使用vue-router构建SPA
    使用自动化构建工具vite搭建新项目#某个目录下执行npmcreatevite@latest 按照提示初始化项目,并按照提示:cdvite-projectnpminstallnpmrundev生成目录结构......
  • Vue3源码阅读梳理
    简单代码例子const{createApp,defineComponent,computed,watch,ref,reactive,effect}=Vueconstapp=createApp({components:[],template:`<div......
  • Vue3+vant+ts 上滑加载,解决上滑调用多次数据的问题
    之前用vue2的时候,写过vue2的用法,链接在这里点击跳转哈,用得挺好的,也没啥问题,照葫芦画瓢的做出来了,但是有问题,下滑之后调用多次数据,按理说组件通过 loading 和 finished......
  • Vue3 Composition API 的优势
     1.OptionsAPI存在的问题使用传统OptionsAPI中,新增或者修改一个需求,就需要分别在data,methods,computed里修改。 2.CompositionAPI的优势我们可以更加优雅的组织......
  • Vue3之provide 与 inject
     provide与inject 作用:实现祖与后代组件间通信,儿子组件中也能用这种方式,但是一般不这么用,父子组件传信息一般直接用props属性。套路:父组件有一个 provide 选项......
  • Vue3之响应式数据的判断
    响应式数据的判断isRef:检查一个值是否为一个ref对象isReactive:检查一个对象是否是由 reactive 创建的响应式代理isReadonly:检查一个对象是否是由 readonly......
  • Vue3之customRef
    customRef作用:创建一个自定义的ref,并对其依赖项跟踪和更新触发进行显式控制。比如在input更新数据之后,设置指定时间之后再在h3标签上重新展示最新的数据:<templ......
  • Vue3之toRaw 与 markRaw
    toRaw与markRawtoRaw:作用:将一个由reactive生成的响应式对象转为普通对象。ref的对象不行使用场景:用于读取响应式对象对应的普通对象,对这个普通对象的所有操作,不会引......