• 在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的菜单配置所以覆盖了原本的默认菜单,如果不自定义将使用默认的菜单配置

    自定义菜单:

  • 相关阅读:
    活动选择问题(动态规划和贪心算法)
    制造业企业防范勒索病毒攻击的一些建议措施
    MFC3d立体按钮制作
    maven安装及配置(详细版)
    【汇编语言-王爽】第八章:数据处理的两个基本问题
    centos7安装zabbix 5.0
    解决webpack报错:You forgot to add ‘mini-css-extract-plugin‘ plugin
    React状态与引用(Refs)- 差异和使用场景
    HashMap中的put()和get()的实现原理
    MogaFX—我附近的货币兑换
  • 原文地址:https://blog.csdn.net/weixin_52941842/article/details/133395686