• Vue3项目引入 vue-quill 编辑器组件并封装使用


    这是一款支持Vue3的富文本编辑器
    GitHub地址:https://github.com/vueup/vue-quill/
    VueQuill官网:https://vueup.github.io/vue-quill/

    1. // 查看 @vueup/vue-quill 版本
    2. npm view @vueup/vue-quill versions --json
    3. // 导入 @vueup/vue-quill 依赖包
    4. npm i @vueup/vue-quill@1.0.0

    一、首先实现父页面【index.vue】

    1. <script>
    2. import QuillEditor from './components/quillEditor'
    3. export default {
    4. name: 'QuillEditorComponent',
    5. components: {
    6. QuillEditor
    7. },
    8. data () {
    9. return {
    10. // 编辑器内容
    11. editorContent: ''
    12. }
    13. },
    14. methods: {
    15. /**
    16. * 设置编辑器内容
    17. */
    18. async handleSetContentClick(evt) {
    19. this.$elementUtil.handleElButtonBlur(evt)
    20. const refs = await this.$refs.quillEditorRef;
    21. const status = await refs.handleSetHtml('

      你好世界!

      '
      )
    22. console.log('handleSetContentClick =>', status)
    23. },
    24. /**
    25. * 获取编辑器内容
    26. */
    27. async handleGetContentClick(evt) {
    28. this.$elementUtil.handleElButtonBlur(evt)
    29. const refs = await this.$refs.quillEditorRef;
    30. this.editorContent = await refs.handleGetHtml();
    31. console.log('handleGetContentClick =>', this.editorContent)
    32. },
    33. }
    34. }
    35. script>

    二、然后设计子组件【quillEditor.vue】

    1. <script>
    2. import { QuillEditor } from '@vueup/vue-quill'
    3. import '@vueup/vue-quill/dist/vue-quill.snow.css'
    4. export default {
    5. components: {
    6. QuillEditor
    7. },
    8. data () {
    9. return {
    10. // 编辑器内容
    11. content: '',
    12. // 编辑器选项
    13. options: {
    14. debug: 'info',
    15. modules: {
    16. toolbar: ['bold', 'italic', 'underline']
    17. },
    18. placeholder: '请输入内容~',
    19. readOnly: false,
    20. theme: 'snow'
    21. }
    22. }
    23. },
    24. methods: {
    25. /**
    26. * 设置编辑器 Text 内容
    27. */
    28. async handleSetText(content) {
    29. const refs = await this.$refs
    30. refs.quill.setText(content)
    31. return 'OK'
    32. },
    33. /**
    34. * 获取编辑器 Text 内容
    35. */
    36. async handleGetText() {
    37. const refs = await this.$refs
    38. return refs.quill.getText();
    39. },
    40. /**
    41. * 设置编辑器 Html 代码
    42. */
    43. async handleSetHtml(content) {
    44. const refs = await this.$refs
    45. refs.quill.setHTML(content)
    46. return 'OK'
    47. },
    48. /**
    49. * 获取编辑器 Html 代码
    50. */
    51. async handleGetHtml() {
    52. const refs = await this.$refs
    53. return refs.quill.getHTML();
    54. }
    55. }
    56. }
    57. script>

    三、效果如下 ~


     

  • 相关阅读:
    百度笔试题面试题总结1
    33.线程中常用的方法【20220811】
    RHCE8 资料整理(三)
    oracle表空间释放
    华为云云耀云服务器 L 实例评测:快速建站的新选择,初创企业和开发者的理想之选
    muduo库剖析(2)
    25装饰器2
    我们为什么需要调用InitCommonControls?
    CLR C#--引用类型和值类型
    linux python 保存图形savefig import matplotlib.pyplot as plt
  • 原文地址:https://blog.csdn.net/Cai181191/article/details/128135901