• 在vue使用wangEditor(简单使用)


    wangEditor不同的版本使用方法都不一样,这里以目前最新的参考官网方法使用2023-09-28

     首先安装,参考官网,注意editor跟editor-for-vue两个都要装

    1. yarn add @wangeditor/editor
    2. # 或者 npm install @wangeditor/editor --save
    3. yarn add @wangeditor/editor-for-vue
    4. # 或者 npm install @wangeditor/editor-for-vue --save

    然后就是参考官网示例操作就好了,官网有现成的代码示例,这里再加点注意事项注释

    全部代码:

    1. <script>
    2. import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
    3. import { DomEditor } from "@wangeditor/editor";
    4. import "@wangeditor/editor/dist/css/style.css"//
    5. export default {
    6. components: { Editor, Toolbar },
    7. data() {
    8. return {
    9. editor: null,
    10. toolbar: null,
    11. html: "",
    12. toolbarConfig: {},
    13. editorConfig: { placeholder: "请输入内容..." },
    14. mode: "simple", // default or 'simple'两种模式
    15. };
    16. },
    17. methods: {
    18. // editor创建时的钩子
    19. onCreated(editor) {
    20. this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错
    21. this.$nextTick(() => {
    22. //一定要用 this.$nextTick包裹起来,等editor实例创建好之后才能获取到toolbar实例
    23. this.toolbar = DomEditor.getToolbar(this.editor);
    24. this.toolbar.getConfig().toolbarKeys //可以查看当前的默认配置
    25. this.editor.getAllMenuKeys()//查询编辑器注册的所有菜单 key
    26. this.toolbar.config.insertKeys = {
    27. index: 4, // 插入的位置,基于当前的 toolbarKeys
    28. keys: ['emotion'],
    29. };
    30. });
    31. this.toolbarConfig.toolbarKeys = [
    32. // 菜单 key或者组的key
    33. 'headerSelect',
    34. // 注意分割线也算一个菜单
    35. '|',
    36. // 菜单 key
    37. 'bold',
    38. 'italic',
    39. 'color',
    40. 'justifyLeft',
    41. 'justifyRight',
    42. 'justifyCenter'
    43. // 继续配置其他菜单...
    44. ]
    45. },
    46. },
    47. mounted() {
    48. // 模拟 ajax 请求,异步渲染编辑器
    49. },
    50. beforeDestroy() {
    51. const editor = this.editor;
    52. if (editor == null) return;
    53. editor.destroy(); // 组件销毁时,及时销毁编辑器
    54. },
    55. };
    56. script>

    最终效果:

    简易模式

    由于我自定义了Toolbar的菜单配置所以覆盖了原本的默认菜单,如果不自定义将使用默认的菜单配置

    自定义菜单:

  • 相关阅读:
    【Linux练习生】线程安全
    如何用wireshark过滤媒体流
    深度学习基础:循环神经网络中的长期依赖问题
    情人节程序员用HTML网页表白【爱心表白】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
    谷歌发布基于声学建模的无限虚拟房间增强现实鲁棒语音识别技术
    【计网 Socket编程】 中科大郑烇老师笔记 (九)
    大数据之Hadoop集群搭建(4个节点)
    Arduino开发实例-PS/2键盘驱动
    airflow重启
    Python list列表修改元素
  • 原文地址:https://blog.csdn.net/weixin_52941842/article/details/133395686