当前位置:   article > 正文

Vue3使用富文本框(wangeditor)_vue3富文本

vue3富文本

毕业涉及中使用到了富文本框,所以学习使用了wangeditor富文本框,现进行总结

1.安装

  1. npm install @wangeditor/editor --save
  2. npm install @wangeditor/editor-for-vue@next --save

2.配置wangeditor组件(src/components/wangeditor.vue)

  1. <template>
  2. <div style="border: 1px solid #ccc">
  3. <Toolbar
  4. style="border-bottom: 1px solid #ccc"
  5. :editor="editorRef"
  6. :defaultConfig="toolbarConfig"
  7. :mode="mode"
  8. />
  9. <Editor
  10. style="min-height: 250px; overflow-y: hidden;"
  11. v-model="valueHtml"
  12. :defaultConfig="editorConfig"
  13. :mode="mode"
  14. @onCreated="handleCreated"
  15. />
  16. </div>
  17. </template>
  1. //script标签中引入
  2. import '@wangeditor/editor/dist/css/style.css' // 引入 css
  3. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  1. export default {
  2. components: { Editor, Toolbar },
  3. setup(props,{emit}) {
  4. emits: ['select']
  5. // 编辑器实例,必须用 shallowRef
  6. const editorRef = shallowRef()
  7. // 内容 HTML
  8. const valueHtml = ref('')
  9. //配置功能栏
  10. let toolbarConfig = {
  11. toolbarKeys: [
  12. 'headerSelect',
  13. 'blockquote',
  14. '|',
  15. 'bold',
  16. 'underline',
  17. 'italic',
  18. {
  19. key: 'group-more-style',
  20. title: '更多',
  21. iconSvg:
  22. '<svg viewBox="0 0 1024 1024"><path d="M204.8 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path><path d="M505.6 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path><path d="M806.4 505.6m-76.8 0a76.8 76.8 0 1 0 153.6 0 76.8 76.8 0 1 0-153.6 0Z"></path></svg>',
  23. menuKeys: ['through', 'code', 'sup', 'sub']
  24. },
  25. 'color',
  26. 'bgColor',
  27. '|',
  28. 'fontSize',
  29. {
  30. key: 'group-justify',
  31. title: '对齐',
  32. iconSvg:
  33. '<svg viewBox="0 0 1024 1024"><path d="M768 793.6v102.4H51.2v-102.4h716.8z m204.8-230.4v102.4H51.2v-102.4h921.6z m-204.8-230.4v102.4H51.2v-102.4h716.8zM972.8 102.4v102.4H51.2V102.4h921.6z"></path></svg>',
  34. menuKeys: ['justifyLeft', 'justifyRight', 'justifyCenter', 'justifyJustify']
  35. },
  36. 'todo',
  37. 'fontFamily',
  38. {
  39. key: 'group-indent',
  40. title: '缩进',
  41. iconSvg:
  42. '<svg viewBox="0 0 1024 1024"><path d="M0 64h1024v128H0z m384 192h640v128H384z m0 192h640v128H384z m0 192h640v128H384zM0 832h1024v128H0z m0-128V320l256 192z"></path></svg>',
  43. menuKeys: ['indent', 'delIndent']
  44. },
  45. '|',
  46. 'emotion',
  47. 'insertLink',
  48. 'uploadImage',
  49. 'insertTable',
  50. 'codeBlock',
  51. 'divider',
  52. 'clearStyle',
  53. '|',
  54. 'undo',
  55. 'redo',
  56. ]
  57. }
  58. const uploadImageList = ref([])
  59. const saveImageList = ref([])
  60. //上传本地图片
  61. function update(file,insertFn) {
  62. let formData = new FormData()
  63. formData.append('file', file)
  64. axios.post('http://localhost:8080/api/file/upload',formData,{
  65. headers: {
  66. 'Content-Type': 'multipart/form-data'
  67. }
  68. }).then(res => {
  69. if (res.data.code == 0){
  70. const src = 'http://121.37.0.16:9000/public/'+ res.data.data.fileName[0]
  71. insertFn(src, '百度 logo', src)
  72. }
  73. })
  74. }
  75. function getOnInsertedImage(imageNode){
  76. uploadImageList.value.push(imageNode)
  77. }
  78. //编辑器配置
  79. let editorConfig = {
  80. placeholder: '请输入内容...',
  81. // 所有的菜单配置,都要在 MENU_CONF 属性下
  82. MENU_CONF: {
  83. insertImage:{
  84. onInsertedImage: getOnInsertedImage()
  85. },
  86. // 配置上传图片
  87. uploadImage: {
  88. customUpload: update
  89. }
  90. }
  91. }
  92. // 组件销毁时,也及时销毁编辑器
  93. onBeforeUnmount(() => {
  94. const editor = editorRef.value
  95. if (editor == null) return
  96. editor.destroy()
  97. })
  98. function copyObject(obj){
  99. return JSON.parse(JSON.stringify(obj));
  100. }
  101. const handleCreated = (editor) => {
  102. editorRef.value = editor // 记录 editor 实例,重要!
  103. saveImageList.value = editor.getElemsByType('image')
  104. uploadImageList.value = copyObject(saveImageList.value)
  105. console.log('created', editor)
  106. }
  107. watch(() => valueHtml.value,()=>{
  108. //当编辑器的内容发生变化时,把值传给父组件
  109. emit('select', valueHtml.value)
  110. })
  111. const handleChange = (editor) => { console.log('change:', editor.children) }
  112. const handleDestroyed = (editor) => { console.log('destroyed', editor) }
  113. const handleFocus = (editor) => { console.log('focus', editor) }
  114. const handleBlur = (editor) => { console.log('blur', editor) }
  115. const customAlert = (info, type) => { alert(`【自定义提示】${type} - ${info}`) }
  116. const customPaste = (editor, event, callback) => {
  117. console.log('ClipboardEvent 粘贴事件对象', event)
  118. // const html = event.clipboardData.getData('text/html') // 获取粘贴的 html
  119. // const text = event.clipboardData.getData('text/plain') // 获取粘贴的纯文本
  120. // const rtf = event.clipboardData.getData('text/rtf') // 获取 rtf 数据(如从 word wsp 复制粘贴)
  121. // 自定义插入内容
  122. editor.insertText('xxx')
  123. // 返回 false ,阻止默认粘贴行为
  124. event.preventDefault()
  125. callback(false) // 返回值(注意,vue 事件的返回值,不能用 return)
  126. // 返回 true ,继续默认的粘贴行为
  127. // callback(true)
  128. }
  129. //父组件调用子组件的方法清空编辑器内容
  130. const abc =function (){
  131. valueHtml.value = ''
  132. }
  133. //暴露该方法,defineExpose要引入
  134. defineExpose({
  135. abc
  136. })
  137. return {
  138. editorRef,
  139. valueHtml,
  140. mode: 'default', // 或 'simple'
  141. toolbarConfig,
  142. editorConfig,
  143. handleCreated,
  144. handleChange,
  145. handleDestroyed,
  146. handleFocus,
  147. handleBlur,
  148. customAlert,
  149. customPaste,
  150. abc,
  151. };
  152. }
  153. }

3.父组件

  1. //引入
  2. import WangEditor from '../../../components/WangEditor.vue'
  3. //注册
  4. components: {
  5. WangEditor
  6. },
  1. <a-form-model-item :wrapper-col="{ offset: 2, span: 24 }" name="introduction">
  2. <div>课程介绍:</div><br>
  3. <WangEditor
  4. class="WangEditor"
  5. @select="getRich"
  6. ref="childrenRef"
  7. />
  8. </a-form-model-item>
  1. //当编辑器的内容更新时,获取该值
  2. const getRich = function (value){
  3. state.introduction = value
  4. console.log(value)
  5. }
  6. //获取dom元素
  7. const childrenRef = ref(null)
  8. ······
  9. //调用子组件的方法清空编辑器内容
  10. childrenRef.value.abc()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/635027
推荐阅读
相关标签
  

闽ICP备14008679号