当前位置: 首页 > 工具软件 > wangEditor > 使用案例 >

wangeditor封装插件初步

商焕
2023-12-01

安装

安装 editor

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

安装 Vue3 组件

yarn add @wangeditor/editor-for-vue@next
# 或者 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 ,getCurrentInstance} from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  const editorRef = shallowRef()

    // 内容 HTML
    const valueHtml = ref('<p>hello</p>')
    const toolbarConfig = {}
    const editorConfig = { placeholder: '请输入内容...' } 
export default {
  components: { Editor, Toolbar },
  data() {
        return {
           editorRef,
            valueHtml,
            mode: 'default', // 或 'simple'
            toolbarConfig,
            editorConfig,
        }
        },
     props:{
	        
            // 绑定值 
            htmlval: {}, 
         
	},   
  mounted() {
       
     this.parseJv()
  },    
  methods:{ 
        parseJv(){
            console.log(this.htmlval)
             setTimeout(() => {
             
            valueHtml.value = this.htmlval;// '<p>模拟 Ajax 异步设置内容</p>'
        }, 500)
        },
         handleCreated(editor) {
            editorRef.value = editor // 记录 editor 实例,重要!
            } 
  },
  BeforeUnmount(){
  const editor = editorRef.value
        if (editor == null) return
        editor.destroy()
  }, 
  setup() {  
    // // 组件销毁时,也及时销毁编辑器 
    // const handleCreated = (editor) => {
    //   editorRef.value = editor // 记录 editor 实例,重要!
    // } 
    // return { 
    //   handleCreated
    // }; 
  }
}
</script>    

引用

<template>
  <WangEditor  :htmlval="htmlStr" />
 
</template>
<script>
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import WangEditor from './components/form/WangEditor.vue'
export default {
  components: {WangEditor },
  data() {
        return { 
            htmlStr: '<p>helloasd1</p>', 
        }
    },
}
</script>
 类似资料: